RegisterTool.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //************************************************************************
  2. // author: yuzhengyang
  3. // date: 2017.3.29 - 2017.6.13
  4. // desc: 注册表操作工具
  5. // Copyright (c) yuzhengyang. All rights reserved.
  6. //************************************************************************
  7. using Azylee.Core.DataUtils.StringUtils;
  8. using Microsoft.Win32;
  9. using System;
  10. namespace Azylee.Core.WindowsUtils.RegisterUtils
  11. {
  12. /// <summary>
  13. /// 注册表操作工具
  14. /// </summary>
  15. public class RegisterTool
  16. {
  17. [Obsolete("SetValue",true)]
  18. public static bool Write(string key, string name, string value)
  19. {
  20. try
  21. {
  22. RegistryKey RKey = Registry.LocalMachine.OpenSubKey(key, true);
  23. if (RKey == null)
  24. RKey = Registry.LocalMachine.CreateSubKey(key);
  25. RKey.SetValue(name, value);
  26. return true;
  27. }
  28. catch (Exception e)
  29. {
  30. return false;
  31. }
  32. }
  33. [Obsolete("GetValue", true)]
  34. public static string Read(string key, string name)
  35. {
  36. try
  37. {
  38. RegistryKey RKey = Registry.LocalMachine.OpenSubKey(key, true);
  39. if (RKey != null)
  40. {
  41. return RKey.GetValue(name) != null ? RKey.GetValue(name).ToString() : "";
  42. }
  43. }
  44. catch (Exception e) { }
  45. return null;
  46. }
  47. [Obsolete("DeleteValue", true)]
  48. public static bool Delete(string key, string name)
  49. {
  50. try
  51. {
  52. RegistryKey RKey = Registry.LocalMachine.OpenSubKey(key, true);
  53. if (RKey != null)
  54. RKey.DeleteValue(name);
  55. return true;
  56. }
  57. catch (Exception e)
  58. {
  59. return false;
  60. }
  61. }
  62. /// <summary>
  63. /// 添加注册表值
  64. /// </summary>
  65. /// <param name="key"></param>
  66. /// <param name="name"></param>
  67. /// <param name="value"></param>
  68. /// <returns></returns>
  69. public static bool SetValue(string key, string name, string value)
  70. {
  71. try
  72. {
  73. using (RegistryKey RKey = Create(key))
  74. {
  75. RKey.SetValue(name, value);
  76. }
  77. return true;
  78. }
  79. catch (Exception e)
  80. {
  81. return false;
  82. }
  83. }
  84. /// <summary>
  85. /// 读取注册表值
  86. /// </summary>
  87. /// <param name="key"></param>
  88. /// <param name="name"></param>
  89. /// <returns></returns>
  90. public static string GetValue(string key, string name)
  91. {
  92. try
  93. {
  94. using (RegistryKey RKey = Open(key, false))
  95. {
  96. if (RKey != null)
  97. {
  98. return RKey.GetValue(name) != null ? RKey.GetValue(name).ToString() : null;
  99. }
  100. }
  101. }
  102. catch (Exception e) { }
  103. return null;
  104. }
  105. /// <summary>
  106. /// 删除注册表值
  107. /// </summary>
  108. /// <param name="key"></param>
  109. /// <param name="name"></param>
  110. /// <returns></returns>
  111. public static bool DeleteValue(string key, string name)
  112. {
  113. try
  114. {
  115. using (RegistryKey RKey = Open(key, true))
  116. {
  117. if (RKey != null)
  118. RKey.DeleteValue(name);
  119. }
  120. return true;
  121. }
  122. catch (Exception e)
  123. {
  124. return false;
  125. }
  126. }
  127. /// <summary>
  128. /// 分离注册表根目录和子目录
  129. /// </summary>
  130. /// <param name="key"></param>
  131. /// <param name="reg"></param>
  132. /// <param name="sub"></param>
  133. /// <returns></returns>
  134. private static bool ExtractInfo(string key, out string reg, out string sub)
  135. {
  136. reg = ""; sub = "";
  137. int splitPos = 1;
  138. if ((splitPos = key.IndexOf('\\')) > 0)
  139. {
  140. reg = key.Substring(0, splitPos);
  141. sub = key.Substring(splitPos + 1);
  142. return true;
  143. }
  144. return false;
  145. }
  146. /// <summary>
  147. /// 打开注册表相应目录
  148. /// </summary>
  149. /// <param name="key">目标子项</param>
  150. /// <param name="writable">是否具有写权限</param>
  151. /// <returns></returns>
  152. private static RegistryKey Open(string key, bool writable)
  153. {
  154. string regkey, subkey;
  155. if (ExtractInfo(key, out regkey, out subkey))
  156. {
  157. switch (regkey)
  158. {
  159. case "HKEY_CLASSES_ROOT": return Registry.ClassesRoot.OpenSubKey(subkey, writable);
  160. case "HKEY_CURRENT_USER": return Registry.CurrentUser.OpenSubKey(subkey, writable);
  161. case "HKEY_LOCAL_MACHINE": return Registry.LocalMachine.OpenSubKey(subkey, writable);
  162. case "HKEY_USERS": return Registry.Users.OpenSubKey(subkey, writable);
  163. case "HKEY_CURRENT_CONFIG": return Registry.CurrentConfig.OpenSubKey(subkey, writable);
  164. default: return Registry.CurrentUser.OpenSubKey(subkey, writable);
  165. }
  166. }
  167. return Registry.CurrentUser.OpenSubKey(subkey, writable);
  168. }
  169. /// <summary>
  170. /// 创建或打开注册表相应目录
  171. /// </summary>
  172. /// <param name="key">目标子项</param>
  173. /// <returns></returns>
  174. private static RegistryKey Create(string key)
  175. {
  176. string regkey, subkey;
  177. if (ExtractInfo(key, out regkey, out subkey))
  178. {
  179. switch (regkey)
  180. {
  181. case "HKEY_CLASSES_ROOT": return Registry.ClassesRoot.CreateSubKey(subkey);
  182. case "HKEY_CURRENT_USER": return Registry.CurrentUser.CreateSubKey(subkey);
  183. case "HKEY_LOCAL_MACHINE": return Registry.LocalMachine.CreateSubKey(subkey);
  184. case "HKEY_USERS": return Registry.Users.CreateSubKey(subkey);
  185. case "HKEY_CURRENT_CONFIG": return Registry.CurrentConfig.CreateSubKey(subkey);
  186. default: return Registry.CurrentUser.CreateSubKey(subkey);
  187. }
  188. }
  189. return Registry.CurrentUser.CreateSubKey(subkey);
  190. }
  191. /// <summary>
  192. /// 判断是否存在项、键、值匹配
  193. /// </summary>
  194. /// <param name="key"></param>
  195. /// <param name="name"></param>
  196. /// <param name="value"></param>
  197. /// <returns></returns>
  198. public static bool Exist(string key, string name, string value)
  199. {
  200. if (Str.Ok(key) && Str.Ok(name))
  201. {
  202. string val = RegisterTool.GetValue(key, name);
  203. if (Str.Ok(value))
  204. {
  205. if (Str.Ok(val) && value == val) return true;
  206. }
  207. else
  208. {
  209. if (val != null) return true;
  210. }
  211. }
  212. return false;
  213. }
  214. }
  215. }