C#:写入注册表开机自动运行

 

 
复制代码
 1   /// <summary>
 2         /// 写进注册表,以便开机启动
 3         /// </summary>
 4         private void SetAutoStar()
 5         {
 6             try
 7             {
 8                 string filepath = Assembly.GetExecutingAssembly().Location;
 9                 string runName = Path.GetFileNameWithoutExtension(filepath);
10 
11                 RegistryKey hkml = Registry.LocalMachine;
12                 RegistryKey runKey = hkml.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
13                 runKey.SetValue(runName, filepath);
14                 runKey.Close();
15             }
16             catch (Exception ex)
17             {
18                 MessageBox.Show(ex.Message);
19             }
20           
21         }
复制代码

取消自动重启,删除相应的注册表

复制代码
 1   /// <summary>
 2         /// 取消开机启动,删除注册表中的值
 3         /// </summary>
 4         public void CancleAutoRun()
 5         {
 6             try
 7             {
 8                 string filepath = Assembly.GetExecutingAssembly().Location;
 9                 string runName = Path.GetFileNameWithoutExtension(filepath);
10                 RegistryKey hkml = Registry.LocalMachine;
11                 RegistryKey runKeys = hkml.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
12                 runKeys.DeleteValue(runName);
13             }
14             catch
15             { }
16         }
复制代码

转载于:https://www.cnblogs.com/wifi/articles/2480376.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值