C# 操作注册表WindowsRegistry
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
namespace BugsBox.Application.Core
{
public class WindowsRegistry
{ const string REGISTRY_ITEM_PATH = "software\\pharmacyinst";
/// <summary>
/// 创建注册表项
/// </summary>
/// <param name="registryItemPath"></param>
public static void CreateRegistry(string registryItemPath = REGISTRY_ITEM_PATH)
{
RegistryKey key = Registry.LocalMachine;
RegistryKey software = key.CreateSubKey(registryItemPath);
//在HKEY_LOCAL_MACHINE\SOFTWARE下新建名为test的注册表项。如果已经存在则不影响!
} /// <summary>
/// 获取注册表值
/// </summary>
/// <returns></returns>
public static string GetRegistryValue(string key, string registryItemPath = REGISTRY_ITEM_PATH)
{
string info = "";
RegistryKey Key;
Key = Registry.LocalMachine;
var myreg = Key.OpenSubKey(registryItemPath);
info = myreg.GetValue(key).ToString();
myreg.Close();
return info;
} /// <summary>
/// 修改注册表项值
/// </summary>
/// <param name="subKey"></param>
/// <param name="val"></param>
/// <param name="registryItemPath"></param>
public static void SetRegistryValue(string subKey, string val, string registryItemPath = REGISTRY_ITEM_PATH)
{
using (RegistryKey key = Registry.LocalMachine)
{
RegistryKey software = key.OpenSubKey(registryItemPath, true); //该项必须已存在
software.SetValue(subKey, val);
//在HKEY_LOCAL_MACHINE\SOFTWARE\test下创建一个名为“test”,值为“博客园”的键值。如果该键值原本已经存在,则会修改替换原来的键值,如果不存在则是创建该键值。
// 注意:SetValue()还有第三个参数,主要是用于设置键值的类型,如:字符串,二进制,Dword等等~~默认是字符串。如:
// software.SetValue("test", "0", RegistryValueKind.DWord); //二进制信息
key.Close();
}
} /// <summary>
/// 删除注册表项
/// </summary>
public static void DeleteRegistry(string registryItemPath = REGISTRY_ITEM_PATH)
{
RegistryKey key = Registry.LocalMachine;
key.DeleteSubKey(registryItemPath, true); //该方法无返回值,直接调用即可
key.Close();
} /// <summary>
/// 删除注册表值
/// </summary>
public static void DeleteRegistryValue(string subKey, string registryItemPath = REGISTRY_ITEM_PATH)
{
RegistryKey delKey = Registry.LocalMachine.OpenSubKey(registryItemPath, true);
delKey.DeleteValue(subKey);
delKey.Close();
} /// <summary>
/// 判断注册表项是否存在
/// </summary>
/// <returns></returns>
public static bool IsRegeditItemExist()
{
string[] subkeyNames;
RegistryKey hkml = Registry.LocalMachine;
RegistryKey software = hkml.OpenSubKey("SOFTWARE");
//RegistryKey software = hkml.OpenSubKey("SOFTWARE", true);
subkeyNames = software.GetSubKeyNames();
//取得该项下所有子项的名称的序列,并传递给预定的数组中
foreach (string keyName in subkeyNames)
//遍历整个数组
{
if (keyName == "test")
//判断子项的名称
{
hkml.Close();
return true;
}
}
hkml.Close();
return false;
}
/// <summary>
/// 判断键值是否存在
/// </summary>
/// <returns></returns>
public static bool IsRegeditKeyExist(string subKey, string registryItemPath = REGISTRY_ITEM_PATH)
{
string[] subkeyNames;
RegistryKey hkml = Registry.LocalMachine;
RegistryKey software = hkml.OpenSubKey(registryItemPath);
//RegistryKey software = hkml.OpenSubKey("SOFTWARE\\test", true);
subkeyNames = software.GetValueNames();
//取得该项下所有键值的名称的序列,并传递给预定的数组中
foreach (string keyName in subkeyNames)
{
if (keyName == subKey) //判断键值的名称
{
hkml.Close();
return true;
} }
hkml.Close();
return false;
}
}
}
C# 操作注册表WindowsRegistry的更多相关文章
- [转]C#操作注册表
原文链接:http://www.cnblogs.com/txw1958/archive/2012/08/01/csharp-regidit.html 下面我们就来用.NET下托管语言C#注册表操作,主 ...
- [荐]使用Js操作注册表
使用Js操作注册表 要操作注册表需要通过ActiveX控件调用WScript.shell对象,通过该对象的一些方法来操作. WshShell对象:可以在本地运行程序.操纵注册表内容.创建快捷方式或访问 ...
- c++ 操作注册表
1. 注册表简介 注册表是为Windows NT和Windows95中所有32位硬件/驱动和32位应用程序设计的数据文件,用于存储系统和应用程序的设置信息.16位驱动在Winnt (Win ...
- .Net操作注册表--un
C#操作注册表 导入命名空间 Using MicroSoft.Win32;//64位系统装的64位版本
- C#操作注册表全攻略
相信每个人对注册表并不陌生,在运行里面输入“regedit”就可以打开注册表编辑器了.这东西对Windows系统来说可是比较重要的,也是病 毒常常会光顾的地方,比如病毒和恶意软件常常会在注册表的启动项 ...
- C#获取cpu序列号 硬盘ID 网卡硬地址以及操作注册表 .
转:http://blog.csdn.net/smartsmile2012/article/details/8682295 #region 获取cpu序列号 硬盘ID 网卡硬地址 /**/ /// & ...
- VBS 操作注册表 十六进制
使用VBS操作注册表,通常使用RegRead/RegWrite/RegDelete方法,如: RegRead: 'read.vbs(将以下代码存为read.vbs文件) Dim OperationRe ...
- win7 64bit+vs2010 操作注册表
注册表五个根键 HKEY_CLASSES_ROOT--管理文件系统 HKEY_LOCAL_MACHINE--管理当前系统硬件配置 HKEY_LOCAL_USER--管理系统当前用户配置 HKEY ...
- How to:Installshield判断操作系统是否为64位,并且为操作注册表进行设置
原文:How to:Installshield判断操作系统是否为64位,并且为操作注册表进行设置 IS脚本操作注册表在64位平台下必须有特殊的设置 if (SYSINFO.bIsWow64) then ...
随机推荐
- vue组件通信新姿势
在vue项目实际开发中我们经常会使用props和emit来进行子父组件的传值通信,父组件向子组件传递数据是通过prop传递的, 子组件传递数据给父组件是通过$emit触发事件来做到的.例如: Vue. ...
- 《python语言程序设计》_第5章_循环
#5.1_引言 程序1: 结果: 程序2: 结果: sum is 45 ##注意整个循环语句都要内缩进循环内部 ##ctrl+c中止一个无限循环运行 程序清单5-1: 结果: #5.2.1_实例研究: ...
- shell脚本新建文件夹或用到目录时多出M或者?之类的
新建问价加多出? 删除多显示M 建立软连接多\n等 可能是文件兼容问题, 1.首先用vi命令打开文件[root@localhost test]# vi test.sh 2.在vi命令模式中使用 ...
- python的基本数据类型(一)
一.运算符逻辑运算and:两边都真则真or:有真则真not:非假是真 顺序:()>==>not==>and>=or 二.while.. elsewhile 条件: 代码块els ...
- JVM之虚拟机类加载机制
有兴趣可以先参考前面的几篇JVM总结: JVM自动内存管理机制-Java内存区域(上) JVM自动内存管理机制-Java内存区域(下) JVM垃圾收集器与内存分配策略(一) 我们知道,在编写一 ...
- C#通过COM组件操作IE浏览器(四):实用代码总结
//执行js方法 IHTMLWindow2 win = oDocument2.parentWindow; win.execScript("functiona();", " ...
- 吴恩达机器学习笔记37-学习曲线(Learning Curves)
学习曲线就是一种很好的工具,我经常使用学习曲线来判断某一个学习算法是否处于偏差.方差问题.学习曲线是学习算法的一个很好的合理检验(sanity check).学习曲线是将训练集误差和交叉验证集误差作为 ...
- NetStandard;.netCore;FX(.netFrameWork)之间引用关系
.NetStandard;.netCore;FX(.NetFrameWork)之间引用关系 FX引用.NetCore:不通过 NetStandard引用.NetCore:不通过 .NetCor ...
- 2.MySQL(二)
数据之表操作 1.创建表 语法:CREATE TABLE table_name (column_name column_type); create table student( -> id IN ...
- Java核心技术及面试指南 键值对方面的面试题总结以及答案
3.3.5.1如何遍历HashMap对象?尤其请说明通过Iterator遍历HashMap对象的方法. 建议用这种方式: Set<Entry<String,String>>en ...