一、添加引用
using Microsoft.Win32;//添加的引用
二、開啓和關閉的方法
string path = Application.ExecutablePath;
RegistryKey rk = Registry.LocalMachine;
RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
if (checkBox1.Checked) //設置開機自啓動
{
rk2.SetValue("JcShutdown", path);
}
else //取消開機自啓動
{
rk2.DeleteValue("JcShutdown", false);
}
rk2.Close();
rk.Close();
三、查詢一下
RegistryKey rk = Registry.LocalMachine;
RegistryKey rk2 = rk.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
string[] subkeyNames;
subkeyNames = rk2.GetValueNames();//取得該項下所有子項的名稱的序列,並傳遞給預定的數組中
foreach (string keyName in subkeyNames) //遍歷整個數組
{
if (keyName == "JcShutdown") //判斷子項的名稱
{
MessageBox.Show("存在");
}
}
rk2.Close();
rk.Close();
----------------------------------------------------------------------------
創建於2016年12月30日
整理於2017年11月28日