C#使用注册表添加删除开机启动项
2012-06-20 11:50
330 查看
添加启动项
RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); if(key == null)//如果该项不存在的话,则创建该子项 { key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"); } key.SetValue("RandomQuotation", Assembly.GetExecutingAssembly().Location); key.Close();其中Assembly.GetExecutingAssembly().Location是获取当前程序的路径,使用了反射技术所以在一开头还需要添加
using System.Reflection;
删除启动项
RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//打开注册表子项 if(key != null) { try { key.DeleteValue("RandomQuotation"); } catch(Exception) { return; } }如果删除值不存在,或者是只读的,会抛出异常.
另外可以使用以下代码监测一值是否存在
object obj= Registry.GetValue("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", "RandomQuotation",null); if(obj==null) { MessageBox.Show("键不存在"); }
相关文章推荐
- c#使用filesystemwatcher实时监控文件目录的添加和删除
- C# 将程序添加到启动项 (写入注册表),及从启动项中删除
- C#操作注册表实现添加/删除可信站点
- C#使用DotNetZip对zip压缩包进行添加删除操作
- MFC利用注册表添加开机启动项和删除开机启动项
- 使用注册表脚本文件(REG)添加、修改或删除Windows注册表项和值
- C# 将程序添加到启动项 (写入注册表),及从启动项中删除
- ADO.NET .net core2.0添加json文件并转化成类注入控制器使用 简单了解 iTextSharp实现HTML to PDF ASP.NET MVC 中 Autofac依赖注入DI 控制反转IOC 了解一下 C# AutoMapper 了解一下
- [c#.net]做tooltip给控件动态添加属性的做法、IExtenderProvider接口的使用
- 【转】c# xml添加 删除 修改数据
- C#对DataGridView中的数据进行添加、修改、删除操作
- 在C#中删除不小心添加的事件
- 使用Quartz进行动态添加、修改和删除Job的工具类
- 使用jquery添加表格行,删除行
- C# 使用IHttpHandler给图片添加水印(图片/文字)
- Android中使用TagFlowLayout制作动态添加删除标签
- c# 添加了按钮双击事件后,再删除掉代码会提示错误
- 使用Bootstrap + Vue.js实现 添加删除数据
- 用批处理文件在注册表中添加开机启动项
- javascript数组元素的添加、删除与插入以及参数数组的使用