--------------------------------------------------------Windows Form1---------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
namespace promegefi
{
public partial class Form1 : Form
{
string lbl = "";
string path = "";
string nombreApp = "";
public string Act1 = Properties.Settings.Default.Actividad1;
public string Act2 = Properties.Settings.Default.Actividad2;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
AutoSizeMode = AutoSizeMode.GrowAndShrink;
btnActivar.Text = "Activar";
timer1.Start();
lblAct1.Text = "Pausa Activa1 - " + Act1;
lblAct2.Text = "Pausa Activa2 - 0" + Act2;
lbl = Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Remove(0, 6) + "\\" + lbl;
nombreApp = "PROMEGEFI";
}
private void Timer1_Tick(object sender, EventArgs e)
{
lblHoraLocal.Text = DateTime.Now.ToLongTimeString();
}
private void Timer2_Tick(object sender, EventArgs e)
{
if (btnActivar.Text == "Re-Activar")
{
if (lblHoraLocal.ToString() == Act2)
{
WindowState = FormWindowState.Normal;
}
}
else if (btnActivar.Text == "Activar")
{
if (lblHoraLocal.ToString() == Act1)
{
WindowState = FormWindowState.Normal;
}
}
}
private void BtnActivar_Click(object sender, EventArgs e)
{
if (btnActivar.Text == "Activar")
{
timer2.Start();
btnActivar.Text = "Re-Activar";
WindowState = FormWindowState.Minimized;
}
else if (btnActivar.Text == "Re-Activar")
{
timer2.Start();
btnActivar.Text = "Activar";
WindowState = FormWindowState.Minimized;
}
}
private void SalirExitToolStripMenuItem_Click(object sender, EventArgs e)
{
string mensaje = "Esta seguro de salir de la aplicación?";
string titulo = "Salir";
MessageBoxButtons botones = MessageBoxButtons.YesNo;
DialogResult result;
result = MessageBox.Show(mensaje, titulo, botones);
if (result == System.Windows.Forms.DialogResult.Yes)
{
Application.Exit();
}
}
private void ActivarToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
// Resgistrará en Inicio del registro la aplicación indicada
// Devuelve True si todo fue bien, False en caso contrario
// Guardar la clave en el registro
// HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
RegistryKey runK = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
// añadirlo al registro
// Si el path contiene espacios se debería incluir entre comillas dobles
if (nombreApp.StartsWith("\"") == false && nombreApp.IndexOf(" ") > -1)
{
nombreApp = "\"" + nombreApp + "\"";
}
runK.SetValue(path, nombreApp);
MessageBox.Show("Ahora PROMEGEFI iniciara automáticamente con Windows!", "Inicio automático REGISTRADO", MessageBoxButtons.OK);
}
catch (Exception ex)
{
Console.WriteLine("ERROR al guardar en el registro.{0}Seguramente no tienes privilegios suficientes.{0}{1}{0}---xxx---{2}", '\n', ex.Message, ex.StackTrace);
}
}
private void DesactivarToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
RegistryKey runK = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
// quitar la clave indicada del registo
runK.DeleteValue(path, false);
MessageBox.Show("Inicio automático desactivado.", "Inicio automático ELIMINADO", MessageBoxButtons.OK);
}
catch (Exception ex)
{
Console.WriteLine("ERROR al eliminar la clave del registro.{0}Seguramente no tienes privilegios suficientes.{0}{1}{0}---xxx---{2}", '\n', ex.Message, ex.StackTrace);
}
}
private void BtnCofiguracion_Click(object sender, EventArgs e)
{
Form2 frmConfig = new Form2();
frmConfig.ShowDialog();
}
}
}
--------------------------------------------------------Windows Form2---------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace promegefi
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Actividad1 = txtAct1.Text;
Properties.Settings.Default.Actividad2 = txtAct2.Text;
Properties.Settings.Default.Save();
string mensaje = "Felicidades, ha configurado su horario!";
string titulo = "Configuración Guardada";
MessageBoxButtons botones = MessageBoxButtons.OK;
DialogResult result;
result = MessageBox.Show(mensaje, titulo, botones);
if (result == System.Windows.Forms.DialogResult.OK)
{
this.Close();
}
}
private void Form2_Load(object sender, EventArgs e)
{
txtAct1.Text = Properties.Settings.Default.Actividad1;
txtAct2.Text = Properties.Settings.Default.Actividad2;
AutoSizeMode = AutoSizeMode.GrowAndShrink;
}
private void Button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}