这是注册表常用的操作:
1 /// <summary> 2 /// 注册表操作类 3 /// </summary> 4 public class WRegisterTool 5 { 6 /// <summary> 7 /// 获得根节点注册表 8 /// </summary> 9 /// <param name="rootKeyType">根节点类型</param> 10 /// <returns></returns> 11 public static RegistryKey GetRootRegisterKey(WRegisterRootKeyType rootKeyType) 12 { 13 switch (rootKeyType) 14 { 15 case WRegisterRootKeyType.HKEY_CLASSES_ROOT: 16 return Registry.ClassesRoot; 17 case WRegisterRootKeyType.HKEY_CURRENT_CONFIG: 18 return Registry.CurrentConfig; 19 case WRegisterRootKeyType.HKEY_CURRENT_USER: 20 return Registry.CurrentUser; 21 case WRegisterRootKeyType.HKEY_LOCAL_MACHINE: 22 return Registry.LocalMachine; 23 default: 24 throw new Exception("注册表类型未定义!"); 25 } 26 } 27 28 /// <summary> 29 /// 在指定注册表项下创建注册表子项 30 /// </summary> 31 /// <param name="fatherKey">父注册表项</param> 32 /// <param name="keyPath">注册表路径</param> 33 /// <returns></returns> 34 public static RegistryKey CreateRegistryKey(RegistryKey fatherKey, string keyPath) 35 { 36 RegistryKey returnKey = fatherKey.CreateSubKey(keyPath); 37 return returnKey; 38 } 39 40 /// <summary> 41 /// 在指定注册表项下创建注册表子项 42 /// </summary> 43 /// <param name="fatherKey">父注册表项</param> 44 /// <param name="keyPath">注册表路径</param> 45 /// <param name="keyValueName">要添加的注册表项值名称</param> 46 /// <param name="keyValue">要添加的注册表项值</param> 47 /// <returns></returns> 48 public static RegistryKey CreateRegistryKey(RegistryKey fatherKey, string keyPath, string keyValueName, string keyValue) 49 { 50 RegistryKey returnKey = CreateRegistryKey(fatherKey, keyPath); 51 returnKey.SetValue(keyValueName, keyValue); 52 return returnKey; 53 } 54 55 /// <summary> 56 /// 在指定注册表项下创建注册表子项 57 /// </summary> 58 /// <param name="rootKeyType">注册表根节点项类型</param> 59 /// <param name="keyPath">注册表路径</param> 60 /// <returns></returns> 61 public static RegistryKey CreateRegistryKey(WRegisterRootKeyType rootKeyType, string keyPath) 62 { 63 RegistryKey rootKey = GetRootRegisterKey(rootKeyType); 64 return CreateRegistryKey(rootKeyType, keyPath); 65 } 66 67 /// <summary> 68 /// 在指定注册表项下创建注册表子项 69 /// </summary> 70 /// <param name="rootKeyType">注册表根节点项类型</param> 71 /// <param name="keyPath">注册表路径</param> 72 /// <param name="keyValueName">要添加的注册表项值名称</param> 73 /// <param name="keyValue">要添加的注册表项值</param> 74 /// <returns></returns> 75 public static RegistryKey CreateRegistryKey(WRegisterRootKeyType rootKeyType, string keyPath, string keyValueName, string keyValue) 76 { 77 RegistryKey returnKey = CreateRegistryKey(rootKeyType, keyPath); 78 returnKey.SetValue(keyValueName, keyValue); 79 return returnKey; 80 } 81 82 /// <summary> 83 /// 删除注册表子项 84 /// </summary> 85 /// <param name="rootKeyType">注册表根节点项类型</param> 86 /// <param name="keyPath">注册表路径</param> 87 /// <returns></returns> 88 public static bool DeleteRegistryKey(WRegisterRootKeyType rootKeyType, string keyPath, string keyName) 89 { 90 RegistryKey rootKey = GetRootRegisterKey(rootKeyType); 91 RegistryKey deleteKey = rootKey.OpenSubKey(keyPath); 92 93 if (IsKeyHaveSubKey(deleteKey, keyName)) 94 { 95 deleteKey.DeleteSubKey(keyName); 96 return true; 97 } 98 return false; 99 } 100 101 102 103 /// <summary> 104 /// 获取注册表项 105 /// </summary> 106 /// <param name="rootKeyType">注册表根节点项类型</param> 107 /// <param name="keyPath">注册表路径</param> 108 /// <returns></returns> 109 public static RegistryKey GetRegistryKey(WRegisterRootKeyType rootKeyType, string keyPath) 110 { 111 RegistryKey rootKey = GetRootRegisterKey(rootKeyType); 112 return rootKey.OpenSubKey(keyPath); 113 } 114 115 116 117 /// <summary> 118 /// 获取注册表项指定值 119 /// </summary> 120 /// <param name="rootKeyType">注册表根节点项类型</param> 121 /// <param name="keyPath">注册表路径</param> 122 /// <param name="keyName">要获得值的注册表值名称</param> 123 /// <returns></returns> 124 public static string GetRegistryValue(WRegisterRootKeyType rootKeyType, string keyPath, string keyName) 125 { 126 RegistryKey key = GetRegistryKey(rootKeyType, keyPath); 127 if (IsKeyHaveValue(key, keyName)) 128 { 129 return key.GetValue(keyName).ToString(); 130 } 131 return null; 132 } 133 134 135 136 /// <summary> 137 /// 设置注册表项值 138 /// </summary> 139 /// <param name="key">注册表项</param> 140 /// <param name="keyValueName">值名称</param> 141 /// <param name="keyValue">值</param> 142 /// <returns></returns> 143 public static bool SetRegistryValue(RegistryKey key, string keyValueName, string keyValue) 144 { 145 key.SetValue(keyValueName, keyValue); 146 return true; 147 } 148 149 150 151 /// <summary> 152 /// 设置注册表项值 153 /// </summary> 154 /// <param name="rootKeyType">注册表根节点项类型</param> 155 /// <param name="keyPath">注册表路径</param> 156 /// <param name="keyValueName">值名称</param> 157 /// <param name="keyValue">值</param> 158 /// <returns></returns> 159 public static bool SetRegistryValue(WRegisterRootKeyType rootKeyType, string keyPath, string keyValueName, string keyValue) 160 { 161 RegistryKey key = GetRegistryKey(rootKeyType, keyPath); 162 key.SetValue(keyValueName, keyValue); 163 return true; 164 } 165 166 167 168 /// <summary> 169 /// 删除注册表项值 170 /// </summary> 171 /// <param name="key">注册表项</param> 172 /// <param name="keyValueName">值名称</param> 173 /// <param name="keyValue">值</param> 174 /// <returns></returns> 175 public static bool DeleteRegistryValue(RegistryKey key, string keyValueName) 176 { 177 if (IsKeyHaveValue(key, keyValueName)) 178 { 179 key.DeleteValue(keyValueName); 180 return true; 181 } 182 return false; 183 } 184 185 186 187 /// <summary> 188 /// 删除注册表项值 189 /// </summary> 190 /// <param name="rootKeyType">注册表根节点项类型</param> 191 /// <param name="keyPath">注册表路径</param> 192 /// <param name="keyValueName">值名称</param> 193 /// <returns></returns> 194 public static bool DeleteRegistryValue(WRegisterRootKeyType rootKeyType, string keyPath, string keyValueName) 195 { 196 RegistryKey key = GetRegistryKey(rootKeyType, keyPath); 197 if (IsKeyHaveValue(key, keyValueName)) 198 { 199 key.DeleteValue(keyValueName); 200 return true; 201 } 202 return false; 203 } 204 205 206 207 /// <summary> 208 /// 判断注册表项是否有指定的注册表值 209 /// </summary> 210 /// <param name="key">注册表项</param> 211 /// <param name="valueName">注册表值</param> 212 /// <returns></returns> 213 private static bool IsKeyHaveValue(RegistryKey key, string valueName) 214 { 215 string[] keyNames = key.GetValueNames(); 216 foreach (string keyName in keyNames) 217 { 218 if (keyName.Trim() == valueName.Trim()) 219 { 220 return true; 221 } 222 } 223 return false; 224 } 225 226 /// <summary> 227 /// 判断注册表项是否有指定的子项 228 /// </summary> 229 /// <param name="key">注册表项</param> 230 /// <param name="keyName">子项名称</param> 231 /// <returns></returns> 232 private static bool IsKeyHaveSubKey(RegistryKey key, string keyName) 233 { 234 string[] subKeyNames = key.GetSubKeyNames(); 235 foreach (string subKeyName in subKeyNames) 236 { 237 if (keyName.Trim() == keyName.Trim()) 238 { 239 return true; 240 } 241 } 242 return false; 243 } 244 245 /// <summary> 246 /// 根据注册表键路径获得注册表键名称 247 /// </summary> 248 /// <param name="keyPath">注册表键路径</param> 249 /// <returns></returns> 250 private static string GetKeyNameByPath(string keyPath) 251 { 252 int keyIndex = keyPath.LastIndexOf("/"); 253 return keyPath.Substring(keyIndex + 1); 254 } 255 256 257 258 259 260 public enum WRegisterRootKeyType 261 { 262 HKEY_CLASSES_ROOT = 0, 263 HKEY_CURRENT_USER = 1, 264 HKEY_LOCAL_MACHINE = 2, 265 HKEY_USERS = 3, 266 HKEY_CURRENT_CONFIG = 4 267 } 268 269