首页  ·  知识 ·  编程语言
设置应用程序开机自动运行
网友  收集  .NET  编辑:德仔   图片来源:网络
设置应用程序开机自动运行 dp-highlighter bar div cl

设置应用程序开机自动运行

C#代码 复制代码
  1. /// <summary>   
  2. /// 设置应用程序开机自动运行   
  3. /// </summary>   
  4. /// <param name="fileName">应用程序的文件名</param>   
  5. /// <param name="isAutoRun">是否自动运行,为false时,取消自动运行</param>   
  6. /// <exception cref="System.Exception">设置不成功时抛出异常</exception>   
  7. public static void SetAutoRun(string fileName, bool isAutoRun)   
  8. {   
  9.     RegistryKey reg = null;   
  10.     try  
  11.     {   
  12.         if (!System.IO.File.Exists(fileName))   
  13.             throw new Exception("该文件不存在!");   
  14.         String name = fileName.Substring(fileName.LastIndexOf(@"\") + 1);   
  15.         reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"true);   
  16.         if (reg == null)   
  17.             reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");   
  18.         if (isAutoRun)   
  19.             reg.SetValue(name, fileName);   
  20.         else  
  21.             reg.SetValue(name, false);   
  22.     }   
  23.     catch (Exception ex)   
  24.     {   
  25.         throw new Exception(ex.ToString());   
  26.     }   
  27.     finally  
  28.     {   
  29.         if (reg != null)   
  30.             reg.Close();   
  31.     }   
  32.   
  33. }  
  34.  
本文作者:网友 来源:网络收集
CIO之家 www.ciozj.com 微信公众号:imciow
    >>频道首页  >>网站首页   纠错  >>投诉
版权声明:CIO之家尊重行业规范,每篇文章都注明有明确的作者和来源;CIO之家的原创文章,请转载时务必注明文章作者和来源;
延伸阅读
也许感兴趣的
我们推荐的
主题最新
看看其它的