通过注册表实现开机自启的取消

一、添加引用

using Microsoft.Win32;//添加的引用

二、开启和关闭的方法

string path = Application.ExecutablePath;
RegistryKey rk = Registry.LocalMachine;
RegistryKey rk2 = rk.CreateSubKey(@"SoftwareMicrosoftWindowsCurrentVersionRun");

if (checkBox1.Checked) //设置开机自启动
{
  rk2.SetValue("JcShutdown", path);
}
else //取消开机自启动
{
  rk2.DeleteValue("JcShutdown", false);
}

rk2.Close();
rk.Close();

三、查询一下

RegistryKey rk = Registry.LocalMachine;
RegistryKey rk2 = rk.CreateSubKey(@"SoftwareMicrosoftWindowsCurrentVersionRun");

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日

原文地址:https://www.cnblogs.com/kanjinxiang/p/7911081.html