C#操作注册表
Posted wangyblzu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#操作注册表相关的知识,希望对你有一定的参考价值。
1.注册表项
直白的理解就是注册表的文件夹项目。对它的操作无非就是增删改查,下面分别进行说明。
(1)查询
/// <summary> /// 判断注册表项是否存在 /// </summary> /// <param name="OpenSubKeyName">注册表项路径</param> /// <param name="itemName">项目名称</param> /// <returns>存在true,不存在false</returns> private bool IsRegeditItemExist(string openSubKeyName, string itemName, EnumRegistryType type) { string[] SubKeyNames; RegistryKey Key = this.GetRegistryType(type); try { if (string.IsNullOrEmpty(openSubKeyName)) { throw new Exception("openSubKeyName is null"); } if (string.IsNullOrEmpty(itemName)) { throw new Exception("itemName is null"); } RegistryKey Software = Key.OpenSubKey(openSubKeyName); if (Software == null) { return false; } SubKeyNames = Software.GetSubKeyNames(); foreach (string keyName in SubKeyNames) { if (keyName == itemName) { Key.Close(); return true; } } Key.Close(); return false; } catch (Exception ex) { Key.Close(); throw new Exception(ex.Message.ToString()); return false; } }
(2)增加
public void CreateRegeditItem(string openSubKeyName, string itemName, EnumRegistryType type) { //存在,返回 if (IsRegeditItemExist(openSubKeyName, itemName, type)) { return; } //不存在,则进行增加操作 RegistryKey Key = Registry.LocalMachine; RegistryKey Software = Key.CreateSubKey(openSubKeyName + "\\\\" + itemName); Key.Close(); return; }
(3)删除
public void DeleteRegeditItem(string openSubKeyName, string itemName, EnumRegistryType type) { //不存在,返回 if (!IsRegeditItemExist(openSubKeyName, itemName, type)) { return; } //存在,则进行删除操作 RegistryKey Key = this.GetRegistryType(type); Key.DeleteSubKey(openSubKeyName + "\\\\" + itemName); Key.Close(); return; }
2.注册表键值
直白的理解就是注册表项里面的各个属性值。对它的操作无非就是增删改查,下面分别进行说明。
(1)查询是否存在
/// <summary> /// 判断注册表键值是否存在 /// </summary> /// <param name="openSubKeyName"></param> /// <param name="keyName"></param> /// <returns></returns> private bool IsRegeditKeyExist(string openSubKeyName, string keyName, EnumRegistryType type) { string[] SubKeyNames; RegistryKey Key = this.GetRegistryType(type); RegistryKey Software = Key.OpenSubKey(openSubKeyName); if (Software == null) { return false; } SubKeyNames = Software.GetValueNames(); foreach (string name in SubKeyNames) { if (name == keyName) { Key.Close(); return true; } } Key.Close(); return false; }
(2)查询值
public string ReadRegeditKey(string openSubKeyName, string keyName, EnumRegistryType type) { if (!IsRegeditKeyExist(openSubKeyName, keyName, type)) { return string.Empty; } RegistryKey Key = this.GetRegistryType(type); RegistryKey myKey = Key.OpenSubKey(openSubKeyName); if (myKey == null) { return string.Empty; } string KeyInfo = myKey.GetValue(keyName).ToString(); Key.Close(); return KeyInfo; }
(3)修改或增加值
public void EditRegieditKey(string openSubKeyName, string keyName, object keyValue, EnumRegistryType type) { if (IsRegeditKeyExist(openSubKeyName, keyName, type)) { return; } RegistryKey Key = this.GetRegistryType(type); RegistryKey Software = Key.OpenSubKey(openSubKeyName, true); if (Software == null) { return; } Software.SetValue(keyName, keyValue); Key.Close(); return; }
(4)删除值
public void DeleteRegieditKey(string openSubKeyName, string keyName, EnumRegistryType type) { if (IsRegeditKeyExist(openSubKeyName, keyName, type)) { return; } RegistryKey Key = this.GetRegistryType(type); RegistryKey Software = Key.OpenSubKey(openSubKeyName, true); if (Software == null) { return; } Software.DeleteValue(keyName); Key.Close(); return; }
3.辅助方法
(1)引用
添加下面一行引用即可调用操作注册表的各个类。
using Microsoft.Win32;
(2)公用方法
只写了一部分,没有写全,仅供参考。
/// <summary> /// /// </summary> /// <param name="type">注册表类型</param> /// <returns></returns> private RegistryKey GetRegistryType(EnumRegistryType type) { RegistryKey Key; switch (type) { case EnumRegistryType.HKEY_CURRENT_USER: Key = Registry.CurrentUser; break; case EnumRegistryType.HKEY_LOCAL_MACHINE: Key = Registry.LocalMachine; break; case EnumRegistryType.HKEY_USER: Key = Registry.Users; break; default: Key = Registry.LocalMachine; break; } return Key; }
(3)枚举类
只写了一部分,没有写全,仅供参考。
public enum EnumRegistryType { /// <summary> /// 当前用户 /// </summary> HKEY_CURRENT_USER, /// <summary> /// 本地主机 /// </summary> HKEY_LOCAL_MACHINE, /// <summary> /// 用户 /// </summary> HKEY_USER }
(4)32位和64位区别
上面默认全部以32位系统为基础,如果是64位系统,则注册表会写入到子项“Wow6432Node”下面。
其中判断是否为64为系统的方法如下:
/// <summary> /// 系统是否64位 /// </summary> /// <returns></returns> private bool Is64Bit() { return System.IntPtr.Size == 8; }
以上是关于C#操作注册表的主要内容,如果未能解决你的问题,请参考以下文章