This article is a mirror article of machine translation, please click here to jump to the original article.

View: 21067|Reply: 0

[Source] Code for booting auto-start exe in .net?

[Copy link]
Posted on 8/14/2015 9:57:20 PM | | |
  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); //取消自动启动
Copy code






Previous:How to set up Lenovo notebook to insert the mouse and close the trackpad
Next:Several simple jquery practice questions with additional answers
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com