#region//開機自動運行
private void CB_Auto_CheckedChanged(object sender, EventArgs e)
{//CB_Auto是一個Checkbox,IsAutoRun 是個布爾變量,用於控制是否開機運行
if (CB_Auto.Checked == true) IsAutoRun = true;
else IsAutoRun = false;
try
{
AutoRun();
}
catch
{ }
}
private void AutoRun()
{
//獲取程序執行路徑..
string starupPath = Application.ExecutablePath;
//class Micosoft.Win32.RegistryKey. 表示Window注冊表中項級節點,此類是注冊表裝.
RegistryKey loca = Registry.LocalMachine;
RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
try
{
//SetValue:存儲值的名稱
if (IsAutoRun == false) run.SetValue("WinForm", false);//取消開機運行
else run.SetValue("WinForm", starupPath);//設置開機運行
loca.Close();
}
catch
{}
}
#endregion
//判斷程序是否已經設置成開機自動啟動,在form_load中寫入
RegistryKey loca_chek = Registry.LocalMachine;
RegistryKey run_Check = loca_chek.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
if (run_Check.GetValue("WinForm").ToString().ToLower() != "false")
{/分別/對應上面的WinForm和false
CB_Auto.Checked = true;
}
else
{
CB_Auto.Checked = false;
}
