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>