C#实现开机自动启动设置代码分享
/// <summary>
/// 设置程序开机启动
/// 或取消开机启动
/// </summary>
/// <param name="started">设置开机启动,或者取消开机启动</param>
/// <param name="exeName">注册表中程序的名字</param>
/// <param name="path">开机启动的程序路径</param>
/// <returns>开启或则停用是否成功</returns>
public static bool runWhenStart(bool started, string exeName, string path)
{
RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//打开注册表子项
if (key == null)//如果该项不存在的话,则创建该子项
{
key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
}
if (started == true)
{
try
{
key.SetValue(exeName, path);//设置为开机启动
key.Close();
}
catch
{
return false;
}
}
else
{
try
{
key.DeleteValue(exeName);//取消开机启动
key.Close();
}
catch
{
return false;
}
}
return true;
}
以上是 C#实现开机自动启动设置代码分享 的全部内容, 来源链接: utcz.com/z/337661.html