Este artículo es un artículo espejo de traducción automática, por favor haga clic aquí para saltar al artículo original.

Vista: 21067|Respuesta: 0

[Fuente] ¿Código para arrancar el exe de inicio automático en .net?

[Copiar enlace]
Publicado en 14/8/2015 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); //取消自动启动
Copiar código






Anterior:Cómo configurar un portátil Lenovo para insertar el ratón y cerrar el trackpad
Próximo:Varias preguntas sencillas de práctica en jquery con respuestas adicionales
Renuncia:
Todo el software, materiales de programación o artículos publicados por Code Farmer Network son únicamente para fines de aprendizaje e investigación; El contenido anterior no se utilizará con fines comerciales o ilegales; de lo contrario, los usuarios asumirán todas las consecuencias. La información de este sitio proviene de Internet, y las disputas de derechos de autor no tienen nada que ver con este sitio. Debes eliminar completamente el contenido anterior de tu ordenador en un plazo de 24 horas desde la descarga. Si te gusta el programa, por favor apoya el software genuino, compra el registro y obtén mejores servicios genuinos. Si hay alguna infracción, por favor contáctanos por correo electrónico.

Mail To:help@itsvse.com