首页 > 编程 > C# > 正文

C#实现开机自动启动设置代码分享

2020-01-24 01:38:49
字体:
来源:转载
供稿:网友
 /// <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;  }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表