C# 使用注册表
一、什么是注册表
注册表是Microsoft Windows操作系统和其应用程序中的一个重要的层次型数据库,用于存储系统和应用程序的设置信息。由键(key,或称“项”)、子键(subkey,子项)和值项(value)构成。一个键就是树状数据结构中的一个节点,而子键就是这个节点的子节点,子键也是键。一个值项则是一个键的一条属性,由名称(name)、数据类型(datatype)以及数据(data)组成。一个键可以有一个或多个值,每个值的名称各不相同,如果一个值的名称为空,则该值为该键的默认值。
(1)、注册表的数据类型主要有以下五种:
(2)、注册表有以下5个一级分支:
(3)、注册表的存储方式:
Windows NT系列操作系统和Windows 9x系列的存储方式有很大区别。注册表被分成多个文件存储,称为Registry Hives,每一个文件被称为一个配置单元。在早期的Windows 3.x系列中,注册表仅包含一个reg.dat文件,所存放的内容后来演变为HKEY_CLASSES_ROOT分支。
Windows NT家族的配置单元文件:
Windows 9x家族的配置单元文件:
二、C#操作注册表
我们可以使用外部加载windows操作系统自带的 Advapi32.dll 文件,实现注册表的操作。下面实例中,我们使用 .NET 平台自带的 Microsoft.Win32.Registry 类库,使用的时候添加如下引用:
using Microsoft.Win32;
具体操作如下:
1 public static class RegistryUtil 2 { 3 #region 创建注册表 4 /// <summary> 5 /// 创建注册表项 6 /// </summary> 7 /// <param name="keyPath"></param> 8 /// <param name="parentKey"></param> 9 /// <returns></returns> 10 public static RegistryKey CreateRegistryKey(string keyPath, RegistryKey parentKey) 11 { 12 return parentKey?.CreateSubKey(keyPath); 13 } 14 15 16 /// <summary> 17 /// 创建<see cref="Registry.LocalMachine">注册表项目 18 /// </summary> 19 /// <param name="keyPath"></param> 20 /// <returns></returns> 21 public static RegistryKey CreateRegistryLocalMachine64Key(string keyPath) 22 { 23 return Registry.LocalMachine.CreateSubKey(keyPath); 24 } 25 26 /// <summary> 27 /// 创建<see cref="Registry.ClassesRoot">注册表项目 28 /// </summary> 29 /// <param name="keyPath"></param> 30 /// <returns></returns> 31 public static RegistryKey CreateRegistryClassesRootKey(string keyPath) 32 { 33 return Registry.ClassesRoot.CreateSubKey(keyPath); 34 } 35 36 /// <summary> 37 /// 创建<see cref="Registry.CurrentConfig">注册表项目 38 /// </summary> 39 /// <param name="keyPath"></param> 40 /// <returns></returns> 41 public static RegistryKey CreateRegistryCurrentConfigKey(string keyPath) 42 { 43 return Registry.CurrentConfig.CreateSubKey(keyPath); 44 } 45 46 /// <summary> 47 /// 创建<see cref="Registry.CurrentUser">注册表项目 48 /// </summary> 49 /// <param name="keyPath"></param> 50 /// <returns></returns> 51 public static RegistryKey CreateRegistryCurrentUserKey(string keyPath) 52 { 53 return Registry.CurrentUser.CreateSubKey(keyPath); 54 } 55 56 /// <summary> 57 /// 创建<see cref="Registry.PerformanceData">注册表项目 58 /// </summary> 59 /// <param name="keyPath"></param> 60 /// <returns></returns> 61 public static RegistryKey CreateRegistryPerformanceDataKey(string keyPath) 62 { 63 return Registry.PerformanceData.CreateSubKey(keyPath); 64 } 65 66 /// <summary> 67 /// 创建<see cref="Registry.Users">注册表项目 68 /// </summary> 69 /// <param name="keyPath"></param> 70 /// <returns></returns> 71 public static RegistryKey CreateRegistryUsersKey(string keyPath) 72 { 73 return Registry.Users.CreateSubKey(keyPath); 74 } 75 #endregion 76 77 78 #region 打开注册表 79 /// <summary> 80 /// 打开 <see cref="RegistryView.Registry64"/> 的 <see cref="RegistryHive.LocalMachine"/> 81 /// </summary> 82 /// <returns></returns> 83 public static RegistryKey OpenLocalMachine64Key() 84 { 85 return RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry64); 86 } 87 88 /// <summary> 89 /// 打开 <see cref="RegistryView.Registry32"/> 的 <see cref="RegistryHive.LocalMachine"/> 90 /// </summary> 91 /// <returns></returns> 92 public static RegistryKey OpenLocalMachine32Key() 93 { 94 return RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32); 95 } 96 97 /// <summary> 98 /// 打开 <see cref="RegistryView.Registry64"/> 的 <see cref="RegistryHive.CurrentUser"/> 99 /// </summary> 100 /// <returns></returns> 101 public static RegistryKey OpenCurrentUser64Key() 102 { 103 return RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.CurrentUser, RegistryView.Registry64); 104 } 105 106 /// <summary> 107 /// 打开 <see cref="RegistryView.Registry32"/> 的 <see cref="RegistryHive.LocalMachine"/> 108 /// </summary> 109 /// <returns></returns> 110 public static RegistryKey OpenCurrentUser32Key() 111 { 112 return RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.CurrentUser, RegistryView.Registry32); 113 } 114 115 /// <summary> 116 /// 打开注册表键 117 /// </summary> 118 /// <param name="rootKey"></param> 119 /// <param name="keyPath"></param> 120 /// <returns></returns> 121 public static RegistryKey OpenKey(this RegistryKey rootKey, string keyPath) 122 { 123 return rootKey.OpenSubKey(keyPath, true); 124 } 125 #endregion 126 127 128 /// <summary> 129 /// 读取注册表值 130 /// </summary> 131 /// <param name="key"></param> 132 /// <param name="name"></param> 133 /// <returns></returns> 134 public static string ReadRegistryValue(this RegistryKey key, string name) 135 { 136 return key?.GetValue(name)?.ToString(); 137 } 138 139 /// <summary> 140 /// 写入注册表值 141 /// </summary> 142 /// <param name="key"></param> 143 /// <param name="name"></param> 144 /// <param name="value"></param> 145 public static void WriteRegistryValue(this RegistryKey key, string name, string value) 146 { 147 key.SetValue(name, value); 148 } 149 150 /// <summary> 151 /// 删除注册值 152 /// </summary> 153 /// <param name="key"></param> 154 /// <param name="name"></param> 155 public static void DeleteRegistryValue(this RegistryKey key, string name) 156 { 157 key.DeleteValue(name); 158 } 159 }
调试代码如下:
1 static void Main(string[] args) 2 { 3 // 写入注册表 4 string path = @"Software\Test\Char"; 5 var key = RegistryUtil.CreateRegistryLocalMachine64Key(path); 6 key.WriteRegistryValue("Name", "Dwayne"); 7 key.WriteRegistryValue("Age", "18"); 8 9 // 读取注册表 10 var regKey = RegistryUtil.OpenLocalMachine64Key().OpenKey(path); 11 var name = regKey.ReadRegistryValue("Name"); 12 var age = regKey.ReadRegistryValue("Age"); 13 Console.WriteLine($"Name={name},Age={age}"); 14 15 Console.WriteLine("Hello World!"); 16 }