private void AutoRun_CheckedChanged(object sender, EventArgs e) { if (AutoRun.Checked)//开机自动启动 { try { RegistryKey runKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); runKey.SetValue("程序名称.exe", System.Windows.Forms.Application.ExecutablePath); runKey.Close(); } catch (IOException) { return ; } } else //不开机自动启动注册表信息删除 { RegistryKey software = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); string[] aimnames = software.GetValueNames(); foreach (string aimKey in aimnames) { if (aimKey.Equals("程序名称.exe")) { software.DeleteValue("程序名称.exe") ; software.Close(); break; } } } }
这篇文档对您是否有帮助?