///
/// 设置指定完整路径下应用程序是否开机运行
/// ///
应用程序完整路径
///
是否开机运行
void SetBootAutoRun(string exePath, bool isruned)
{
//应用程序完整路径Application.ExecutablePath;
//获取应用程序文件名
string filename = exePath.Substring(exePath.LastIndexOf("\\") + 1);
if (isruned)
{//开机运行程序
if (!File.Exists(exePath))//指定文件是否存在
return;
Microsoft.Win32.RegistryKey Rkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (Rkey == null)
Rkey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
Rkey.SetValue(filename, exePath);//修改注册表,使程序开机时自动执行。
}
else
{//关闭开机运行
//修改注册表,使程序开机时不自动执行。
Microsoft.Win32.RegistryKey Rkey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
Rkey.DeleteValue(filename, false);
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/23109131/viewspace-664522/
,如需转载,请注明出处,否则将追究法律责任。