架构师_程序员_码农网

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

搜索
查看: 19943|回复: 0

[资料] .net中开机自动启动exe的代码?

[复制链接]
发表于 2015-8-14 21:57:20 | 显示全部楼层 |阅读模式
  1. public void SetAutoRun(string fileName, bool isAutoRun)   
  2. {   
  3.     RegistryKey reg = null;   
  4.     try
  5.     {   
  6.         if (!System.IO.File.Exists(fileName))   
  7.             throw new Exception("该文件不存在!");   
  8.         String name = fileName.Substring(fileName.LastIndexOf(@"") + 1);   
  9.         reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);   
  10.         if (reg == null)   
  11.             reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");   
  12.         if (isAutoRun)   
  13.             reg.SetValue(name, fileName);   
  14.         else
  15.             reg.SetValue(name, false);   
  16.         lbl_autorunerr.Visible = false;   
  17.     }   
  18.     catch
  19.     {   
  20.         lbl_autorunerr.Visible = true;   
  21.         //throw new Exception(ex.ToString());   
  22.     }   
  23.     finally
  24.     {   
  25.         if (reg != null)   
  26.             reg.Close();   
  27.     }   
  28. }
  29.         public void SetAutoRun(string fileName, bool isAutoRun)
  30.         {
  31.             RegistryKey reg = null;
  32.             try
  33.             {
  34.                 if (!System.IO.File.Exists(fileName))
  35.                     throw new Exception("该文件不存在!");
  36.                 String name = fileName.Substring(fileName.LastIndexOf(@"") + 1);
  37.                 reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
  38.                 if (reg == null)
  39.                     reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
  40.                 if (isAutoRun)
  41.                     reg.SetValue(name, fileName);
  42.                 else
  43.                     reg.SetValue(name, false);
  44.                 lbl_autorunerr.Visible = false;
  45.             }
  46.             catch
  47.             {
  48.                 lbl_autorunerr.Visible = true;
  49.                 //throw new Exception(ex.ToString());
  50.             }
  51.             finally
  52.             {
  53.                 if (reg != null)
  54.                     reg.Close();
  55.             }
  56.         }

  57. 使用的时候,直接调用SetAutoRun函数即可,这里解释一下两个参数的含义:

  58. fileName:需要设置自动启动程序的路径,若为当前程序可直接传递Application.ExecutablePath。

  59. isAutoRun:是否自动运行,为false时,取消自动运行。

  60. 例: SetAutoRun(Application.ExecutablePath, true); //设置自动启动当前程序

  61. SetAutoRun(Application.ExecutablePath, false); //取消自动启动
复制代码






上一篇:联想笔记本怎么设置插入鼠标关闭触控板
下一篇:几道简单的jquery练习题附加答案
码农网,只发表在实践过程中,遇到的技术难题,不误导他人。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

免责声明:
码农网所发布的一切软件、编程资料或者文章仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。

Mail To:help@itsvse.com

QQ|手机版|小黑屋|架构师 ( 鲁ICP备14021824号-2 )|网站地图

GMT+8, 2025-9-17 05:36

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表