这篇文章主要介绍了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;
- }
新闻热点
疑难解答