C# 通用注册表操作方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# 通用注册表操作方法相关的知识,希望对你有一定的参考价值。
/// <summary> /// 注册表帮助类 /// 从读、写、删除、判断 /// 四个方面实现对注册表的简单操作 /// </summary> public class RegeditHelper { /// <summary> /// 读取指定名称的注册表的值 /// 读取的注册表中HKEY_LOCAL_MACHINE\SOFTWARE目录下的XXX目录中名称为name的注册表值 /// </summary> /// <param name="name">注册名称</param> /// <param name="parentKeyName">要打开的子项的名称或路径</param> /// <returns></returns> public static object GetRegistData(string name, string parentKeyName = "SOFTWARE") { try { RegistryKey hkml = Registry.LocalMachine; RegistryKey software = hkml.OpenSubKey(parentKeyName, true); var registData = software.GetValue(name); hkml.Close(); return registData; } catch (Exception ex) { return "System Exceptions:" + ex.Message; } } /// <summary> /// 向注册表中写数据 /// 在注册表中HKEY_LOCAL_MACHINE\SOFTWARE目录下新建XXX目录并在此目录下创建名称为name值为tovalue的注册表项 /// </summary> /// <param name="name">注册表名称</param> /// <param name="tovalue">值</param> /// <param name="parentKeyName">要打开的子项的名称或路径</param> public static void WriteRegedit(string name, object tovalue, string parentKeyName = "SOFTWARE") { try { RegistryKey hklm = Registry.LocalMachine; RegistryKey software = hklm.OpenSubKey(parentKeyName,true); software.SetValue(name, tovalue); hklm.Close(); } catch (Exception ex) { return; } } /// <summary> /// 更新注册表项 /// </summary> /// <param name="name">键</param> /// <param name="tovalue">值</param> /// <param name="parentKeyName">父级名称</param> public static void UpdateRegedit(string name, string tovalue, string parentKeyName = "SOFTWARE") { try { RegistryKey hklm = Registry.LocalMachine; RegistryKey software = hklm.OpenSubKey(parentKeyName, true); software.SetValue(name, tovalue); hklm.Flush(); hklm.Close(); } catch (Exception ex) { return; } } /// <summary> /// 创建子项 /// </summary> /// <param name="name">子项名称</param> /// <param name="parentKeyName">子项的父级</param> public static void CreateRegistItem(string name, string parentKeyName = "SOFTWARE") { try { RegistryKey hklm = Registry.LocalMachine; //RegistryKey software = hklm.CreateSubKey(parentKeyName + "\\" + name, RegistryKeyPermissionCheck.ReadWriteSubTree); RegistryKey software = hklm.OpenSubKey(parentKeyName,true); software.CreateSubKey(name); hklm.Flush(); hklm.Close(); } catch (Exception ex) { return; } } /// <summary> /// 删除注册表中指定的注册表项 /// 在注册表中HKEY_LOCAL_MACHINE\SOFTWARE目录下XXX目录中删除名称为name注册表项 /// </summary> /// <param name="name">注册表名称</param> /// <param name="parentKeyName">要打开的子项的名称或路径</param> public static void DeleteRegist(string name, string parentKeyName = "SOFTWARE") { try { RegistryKey hkml = Registry.LocalMachine; RegistryKey software = hkml.OpenSubKey(parentKeyName, true); var aimnames = software.GetSubKeyNames(); foreach (string aimKey in aimnames) { if (aimKey == name) software.DeleteSubKeyTree(name); } hkml.Close(); } catch (Exception ex) { return; } } /// <summary> /// 判断指定注册表项是否存在 /// 在注册表中HKEY_LOCAL_MACHINE\SOFTWARE目录下XXX目录中判断名称为name注册表项是否存在 /// 这一方法在删除注册表时已经存在 /// 在新建一注册表项时也应有相应判断 /// </summary> /// <param name="name">注册表名称</param> /// <param name="parentKeyName">要打开的子项的名称或路径</param> /// <returns></returns> public static bool IsRegeditKeyExit(string name, string parentKeyName = "SOFTWARE") { try { RegistryKey hkml = Registry.LocalMachine; RegistryKey software = hkml.OpenSubKey(parentKeyName, true); var subkeyNames = software.GetValueNames(); foreach (string keyName in subkeyNames) { if (keyName.ToUpper() == name.ToUpper()) { hkml.Close(); return true; } } hkml.Close(); return false; } catch (Exception ex) { return false; } } /// <summary> /// 判断是否存在子节点 /// </summary> /// <param name="name">注册表子节点名称</param> /// <param name="parentKeyName">要打开的子项的名称或路径</param> /// <returns></returns> public static bool IsRegeditItemExit(string name, string parentKeyName = "SOFTWARE") { try { RegistryKey hkml = Registry.LocalMachine; RegistryKey software = hkml.OpenSubKey(parentKeyName, true); var subkeyNames = software.GetSubKeyNames(); foreach (string keyName in subkeyNames) { if (keyName.ToUpper() == name.ToUpper()) { hkml.Close(); return true; } } hkml.Close(); return false; } catch (Exception ex) { return false; } } }
以上是关于C# 通用注册表操作方法的主要内容,如果未能解决你的问题,请参考以下文章