C#制作开机自动启动程序

1、新建一个类,设置一个操作注册表的方法;

成都创新互联公司是工信部颁发资质IDC服务器商,为用户提供优质的绵阳机房托管服务

2、在程序中调用这个方法。true就自动启动,false就删除自动启动。

  1. public static bool RunWhenStart(bool started, string exeName, string path) 
  2. {   //started是否开机启动,exeName应用程序名称,path应用程序路径 
  3.     RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//打开注册表子项 
  4.     if (key == null) //如果该项不存在的话,则创建该子项 
  5.     { 
  6.         key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"); 
  7.     } 
  8.     if (started == true) 
  9.     { 
  10.         try 
  11.         { 
  12.             key.SetValue(exeName, path);//设置为开机启动 
  13.             key.Close(); 
  14.         } 
  15.         catch 
  16.         { 
  17.             return false; 
  18.         } 
  19.     } 
  20.     else 
  21.     { 
  22.         try 
  23.         { 
  24.             key.DeleteValue(exeName);//取消开机启动 
  25.             key.Close(); 
  26.         } 
  27.         catch 
  28.         { 
  29.             return false; 
  30.         } 
  31.     } 
  32.     return true; 


  1. private void Form1_Load(object sender, EventArgs e) 
  2.     if (Autorun == "true") 
  3.     { 
  4.         function.RunWhenStart(true, "51cto.iego.net", Application.ExecutablePath); 
  5.     } 
  6.     else 
  7.     { 
  8.         function.RunWhenStart(false, "51cto.iego.net", Application.ExecutablePath); 
  9.     } 

 


本文标题:C#制作开机自动启动程序
分享地址:http://lszwz.com/article/ppsedc.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款