码迷,mamicode.com
首页 > Windows程序 > 详细

C# winform 开机自启动

时间:2017-06-27 17:01:01      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:app   div   注册表   删除   registry   关闭   自启动   开机   current   

开机启动

 1             // 获得应用程序路径
 2             string strAssName = Application.StartupPath + @"\" + Application.ProductName + @".exe";
 3             // 获得应用程序名称
 4             string strShortFileName = Application.ProductName;
 5 
 6             // 打开注册表基项"HKEY_LOCAL_MACHINE"
 7             RegistryKey rgkRun = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
 8             if (rgkRun == null)
 9             {   // 若不存在,创建注册表基项"HKEY_LOCAL_MACHINE"
10                 rgkRun = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
11                 MessageBox.Show("添加开机启动成功");
12             }
13 
14             // 设置指定的注册表项的指定名称/值对。如果指定的项不存在,则创建该项。
15             rgkRun.SetValue(strShortFileName, strAssName);
16             MessageBox.Show("添加开机启动成功");

关闭开机启动

 1 // 获得应用程序名称
 2             string strShortFileName = Application.ProductName;
 3 
 4             // 打开注册表基项"HKEY_LOCAL_MACHINE"
 5             RegistryKey rgkRun = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
 6             if (rgkRun == null)
 7             {   // 若不存在,创建注册表基项"HKEY_LOCAL_MACHINE"
 8                 rgkRun = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
 9                 MessageBox.Show("已关闭开机启动");
10             }
11 
12             // 删除指定的注册表项的指定名称/值对。
13             rgkRun.DeleteValue(strShortFileName, false);
14             MessageBox.Show("已关闭开机启动");

 

C# winform 开机自启动

标签:app   div   注册表   删除   registry   关闭   自启动   开机   current   

原文地址:http://www.cnblogs.com/bigebo/p/7085829.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!