public partial class Frm_SetUp : Form
{
public Frm_SetUp()
{
InitializeComponent();
}
//OilAndGasRecovery
public static string SoftWare = "OilAndGasRecovery";
public static bool Journal = true;
//自定义节点名称
// public static string NodeName = "XX";
/// <summary>
/// 打开开机自启动
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cbx_startup_CheckedChanged(object sender, EventArgs e)
{
if (cbx_startup.Checked)
{
//没加try catch 去掉注释就能使用,但不能捕获异常
//MessageBox.Show("设置开机自启动,需要修改注册表", "提示");
//string path = Application.ExecutablePath;
//RegistryKey rk = Registry.CurrentUser; //
// 添加到 当前登陆用户的 注册表启动项
//RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
//rk2.SetValue("1111", path);
//rk2.Close();
//rk.Close();
MessageBox.Show("设置开机自启动,需要修改注册表", "提示");
string path = Application.ExecutablePath;
RegistryKey rk = Registry.CurrentUser; //
// 添加到 当前登陆用户的 注册表启动项
try
{
//
//SetValue:存储值的名称
RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
rk2.SetValue(SoftWare, path);
ConfigText.UpdateAppConfig("SelfStarting", "1");
MessageBox.Show("添加成功");
rk2.Close();
rk.Close();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString(), "提 示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
//没加try catch 去掉注释就能使用,但不能捕获异常
//MessageBox.Show("取消开机自启动,需要修改注册表", "提示");
//string path = Application.ExecutablePath;
//RegistryKey rk = Registry.CurrentUser;
//RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
//rk2.DeleteValue("1111", false);
//rk2.Close();
//rk.Close();
////取消开机自启动
MessageBox.Show("取消开机自启动,需要修改注册表", "提示");
string path = Application.ExecutablePath;
RegistryKey rk = Registry.CurrentUser;
try
{
////SetValue:存储值的名称
RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
rk2.DeleteValue(SoftWare, false);
ConfigText.UpdateAppConfig("SelfStarting", "0");
MessageBox.Show("取消成功");
rk2.Close();
rk.Close();
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString(), "提 示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void cbx_Close_CheckedChanged(object sender, EventArgs e)
{
}
private void Frm_SetUp_Load(object sender, EventArgs e)
{
string str = ConfigurationManager.AppSettings["SelfStarting"];
if (str.Equals("0")) {
cbx_startup.Checked = false;
}
else
{
cbx_startup.Checked = true;
}
//MessageBox.Show(str);
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("是否要恢复默认设置?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
cbx_startup.Checked = false;
cbx_startup_CheckedChanged( sender, e);
}
}
app.config
<appSettings>
<!--开机自启动-->
<add key="SelfStarting" value="0"/>
</appSettings>