이 글은 기계 번역의 미러 문서이며, 원본 기사로 바로 이동하려면 여기를 클릭해 주세요.

보기: 21067|회답: 0

[출처] .net에서 자동 시작 exe를 부팅하는 코드가 있나요?

[링크 복사]
게시됨 2015. 8. 14. 오후 9: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); //取消自动启动
코드 복사






이전의:Lenovo 노트북에서 마우스를 삽입하고 트랙패드를 닫는 설정 방법
다음:추가 답변이 포함된 간단한 jquery 연습 문제 몇 개
면책 조항:
Code Farmer Network에서 발행하는 모든 소프트웨어, 프로그래밍 자료 또는 기사는 학습 및 연구 목적으로만 사용됩니다; 위 내용은 상업적 또는 불법적인 목적으로 사용되지 않으며, 그렇지 않으면 모든 책임이 사용자에게 부담됩니다. 이 사이트의 정보는 인터넷에서 가져온 것이며, 저작권 분쟁은 이 사이트와는 관련이 없습니다. 위 내용은 다운로드 후 24시간 이내에 컴퓨터에서 완전히 삭제해야 합니다. 프로그램이 마음에 드신다면, 진짜 소프트웨어를 지원하고, 등록을 구매하며, 더 나은 진짜 서비스를 받아주세요. 침해가 있을 경우 이메일로 연락해 주시기 바랍니다.

Mail To:help@itsvse.com