C#用注册表开机自动启动某某软件

时间:2023-12-11 20:58:50

代码如下:

     public static void chkAutoRun(bool isRun)
{
if (isRun)//开机自动启动
{
try
{
RegistryKey runKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
runKey.SetValue("AutoRun.exe", System.Windows.Forms.Application.ExecutablePath);
runKey.Close();
this.Text = "注册表修改自启(已开启)";
}
catch (IOException ie)
{
MessageBox.Show(ie.Message);
}
}
else //不开机自动启动注册表信息删除
{
RegistryKey software = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
string[] aimnames = software.GetValueNames();
foreach (string aimKey in aimnames)
{
if (aimKey.Equals("AutoRun.exe"))
{
software.DeleteValue("AutoRun.exe");
software.Close();
break;
}
}
this.Text = "注册表修改自启(已关闭)";
} }