星火批注
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Common.system
  8. {
  9. /// <summary>
  10. /// 注册表操作类
  11. ///1.创建注册表项
  12. ///2.读取注册表项
  13. ///3.判断注册表项是否存在
  14. ///4.删除注册表项
  15. ///5.创建注册表键值
  16. ///6.读取注册表键值
  17. ///7.判断注册表键值是否存在
  18. ///8.删除注册表键值
  19. /// </summary>
  20. public class RegistryHelper
  21. {
  22. #region 字段定义
  23. /// <summary>
  24. /// 注册表项名称
  25. /// </summary>
  26. private string _subkey;
  27. /// <summary>
  28. /// 注册表基项域
  29. /// </summary>
  30. private RegDomain _domain;
  31. /// <summary>
  32. /// 注册表键值
  33. /// </summary>
  34. private string _regeditkey;
  35. #endregion
  36. #region 属性
  37. /// <summary>
  38. /// 设置注册表项名称
  39. /// </summary>
  40. public string SubKey
  41. {
  42. //get { return _subkey; }
  43. set { _subkey = value; }
  44. }
  45. /// <summary>
  46. /// 注册表基项域
  47. /// </summary>
  48. public RegDomain Domain
  49. {
  50. ///get { return _domain; }
  51. set { _domain = value; }
  52. }
  53. /// <summary>
  54. /// 注册表键值
  55. /// </summary>
  56. public string RegeditKey
  57. {
  58. ///get{return _regeditkey;}
  59. set { _regeditkey = value; }
  60. }
  61. #endregion
  62. #region 构造函数
  63. public RegistryHelper()
  64. {
  65. ///默认注册表项名称
  66. _subkey = "software\\";
  67. ///默认注册表基项域
  68. _domain = RegDomain.LocalMachine;
  69. }
  70. /// <summary>
  71. /// 构造函数
  72. /// </summary>
  73. /// <param name="subKey">注册表项名称</param>
  74. /// <param name="regDomain">注册表基项域</param>
  75. public RegistryHelper(string subKey, RegDomain regDomain)
  76. {
  77. ///设置注册表项名称
  78. _subkey = subKey;
  79. ///设置注册表基项域
  80. _domain = regDomain;
  81. }
  82. #endregion
  83. #region 公有方法
  84. #region 创建注册表项
  85. /// <summary>
  86. /// 创建注册表项,默认创建在注册表基项 HKEY_LOCAL_MACHINE 下面(请先设置SubKey 属性)
  87. /// 虚方法,子类可进行重写
  88. /// </summary>
  89. public virtual void CreateSubKey()
  90. {
  91. ///判断注册表项名称是否为空,如果为空,返回false
  92. if (_subkey == string.Empty || _subkey == null)
  93. {
  94. return;
  95. }
  96. ///创建基于注册表基项的节点
  97. RegistryKey key = GetRegDomain(_domain);
  98. ///要创建的注册表项的节点
  99. RegistryKey sKey;
  100. if (!IsSubKeyExist())
  101. {
  102. sKey = key.CreateSubKey(_subkey);
  103. }
  104. //sKey.Close();
  105. ///关闭对注册表项的更改
  106. key.Close();
  107. }
  108. /// <summary>
  109. /// 创建注册表项,默认创建在注册表基项 HKEY_LOCAL_MACHINE 下面
  110. /// 虚方法,子类可进行重写
  111. /// 例子:如subkey 是software\\higame\\,则将创建HKEY_LOCAL_MACHINE\\software\\higame\\注册表项
  112. /// </summary>
  113. /// <param name="subKey">注册表项名称</param>
  114. public virtual void CreateSubKey(string subKey)
  115. {
  116. ///判断注册表项名称是否为空,如果为空,返回false
  117. if (subKey == string.Empty || subKey == null)
  118. {
  119. return;
  120. }
  121. ///创建基于注册表基项的节点
  122. RegistryKey key = GetRegDomain(_domain);
  123. ///要创建的注册表项的节点
  124. RegistryKey sKey;
  125. if (!IsSubKeyExist(subKey))
  126. {
  127. sKey = key.CreateSubKey(subKey);
  128. }
  129. //sKey.Close();
  130. ///关闭对注册表项的更改
  131. key.Close();
  132. }
  133. /// <summary>
  134. /// 创建注册表项,默认创建在注册表基项 HKEY_LOCAL_MACHINE 下面
  135. /// 虚方法,子类可进行重写
  136. /// </summary>
  137. /// <param name="regDomain">注册表基项域</param>
  138. public virtual void CreateSubKey(RegDomain regDomain)
  139. {
  140. ///判断注册表项名称是否为空,如果为空,返回false
  141. if (_subkey == string.Empty || _subkey == null)
  142. {
  143. return;
  144. }
  145. ///创建基于注册表基项的节点
  146. RegistryKey key = GetRegDomain(regDomain);
  147. ///要创建的注册表项的节点
  148. RegistryKey sKey;
  149. if (!IsSubKeyExist(regDomain))
  150. {
  151. sKey = key.CreateSubKey(_subkey);
  152. }
  153. //sKey.Close();
  154. ///关闭对注册表项的更改
  155. key.Close();
  156. }
  157. /// <summary>
  158. /// 创建注册表项(请先设置SubKey 属性)
  159. /// 虚方法,子类可进行重写
  160. /// 例子:如regDomain 是HKEY_LOCAL_MACHINE,subkey 是software\\higame\\,则将创建HKEY_LOCAL_MACHINE\\software\\higame\\注册表项
  161. /// </summary>
  162. /// <param name="subKey">注册表项名称</param>
  163. /// <param name="regDomain">注册表基项域</param>
  164. public virtual void CreateSubKey(string subKey, RegDomain regDomain)
  165. {
  166. ///判断注册表项名称是否为空,如果为空,返回false
  167. if (subKey == string.Empty || subKey == null)
  168. {
  169. return;
  170. }
  171. ///创建基于注册表基项的节点
  172. RegistryKey key = GetRegDomain(regDomain);
  173. ///要创建的注册表项的节点
  174. RegistryKey sKey;
  175. if (!IsSubKeyExist(subKey, regDomain))
  176. {
  177. sKey = key.CreateSubKey(subKey);
  178. }
  179. //sKey.Close();
  180. ///关闭对注册表项的更改
  181. key.Close();
  182. }
  183. #endregion
  184. #region 判断注册表项是否存在
  185. /// <summary>
  186. /// 判断注册表项是否存在,默认是在注册表基项HKEY_LOCAL_MACHINE 下判断(请先设置SubKey 属性)
  187. /// 虚方法,子类可进行重写
  188. /// 例子:如果设置了Domain 和SubKey 属性,则判断Domain\\SubKey,否则默认判断HKEY_LOCAL_MACHINE\\software\\
  189. /// </summary>
  190. /// <returns>返回注册表项是否存在,存在返回true,否则返回false</returns>
  191. public virtual bool IsSubKeyExist()
  192. {
  193. ///判断注册表项名称是否为空,如果为空,返回false
  194. if (_subkey == string.Empty || _subkey == null)
  195. {
  196. return false;
  197. }
  198. ///检索注册表子项
  199. ///如果sKey 为null,说明没有该注册表项不存在,否则存在
  200. RegistryKey sKey = OpenSubKey(_subkey, _domain);
  201. if (sKey == null)
  202. {
  203. return false;
  204. }
  205. return true;
  206. }
  207. /// <summary>
  208. /// 判断注册表项是否存在,默认是在注册表基项HKEY_LOCAL_MACHINE 下判断
  209. /// 虚方法,子类可进行重写
  210. /// 例子:如subkey 是software\\higame\\,则将判断HKEY_LOCAL_MACHINE\\software\\higame\\注册表项是否存在
  211. /// </summary>
  212. /// <param name="subKey">注册表项名称</param>
  213. /// <returns>返回注册表项是否存在,存在返回true,否则返回false</returns>
  214. public virtual bool IsSubKeyExist(string subKey)
  215. {
  216. ///判断注册表项名称是否为空,如果为空,返回false
  217. if (subKey == string.Empty || subKey == null)
  218. {
  219. return false;
  220. }
  221. ///检索注册表子项
  222. ///如果sKey 为null,说明没有该注册表项不存在,否则存在
  223. RegistryKey sKey = OpenSubKey(subKey);
  224. if (sKey == null)
  225. {
  226. return false;
  227. }
  228. return true;
  229. }
  230. /// <summary>
  231. /// 判断注册表项是否存在
  232. /// 虚方法,子类可进行重写
  233. /// 例子:如regDomain 是HKEY_CLASSES_ROOT,则将判断HKEY_CLASSES_ROOT\\SubKey 注册表项是否存在
  234. /// </summary>
  235. /// <param name="regDomain">注册表基项域</param>
  236. /// <returns>返回注册表项是否存在,存在返回true,否则返回false</returns>
  237. public virtual bool IsSubKeyExist(RegDomain regDomain)
  238. {
  239. ///判断注册表项名称是否为空,如果为空,返回false
  240. if (_subkey == string.Empty || _subkey == null)
  241. {
  242. return false;
  243. }
  244. ///检索注册表子项
  245. ///如果sKey 为null,说明没有该注册表项不存在,否则存在
  246. RegistryKey sKey = OpenSubKey(_subkey, regDomain);
  247. if (sKey == null)
  248. {
  249. return false;
  250. }
  251. return true;
  252. }
  253. /// <summary>
  254. /// 判断注册表项是否存在(请先设置SubKey 属性)
  255. /// 虚方法,子类可进行重写
  256. /// 例子:如regDomain 是HKEY_CLASSES_ROOT,subkey 是software\\higame\\,则将判断HKEY_CLASSES_ROOT\\software\\higame\\注册表项是否存在
  257. /// </summary>
  258. /// <param name="subKey">注册表项名称</param>
  259. /// <param name="regDomain">注册表基项域</param>
  260. /// <returns>返回注册表项是否存在,存在返回true,否则返回false</returns>
  261. public virtual bool IsSubKeyExist(string subKey, RegDomain regDomain)
  262. {
  263. ///判断注册表项名称是否为空,如果为空,返回false
  264. if (subKey == string.Empty || subKey == null)
  265. {
  266. return false;
  267. }
  268. ///检索注册表子项
  269. ///如果sKey 为null,说明没有该注册表项不存在,否则存在
  270. RegistryKey sKey = OpenSubKey(subKey, regDomain);
  271. if (sKey == null)
  272. {
  273. return false;
  274. }
  275. return true;
  276. }
  277. #endregion
  278. #region 删除注册表项
  279. /// <summary>
  280. /// 删除注册表项(请先设置SubKey 属性)
  281. /// 虚方法,子类可进行重写
  282. /// </summary>
  283. /// <returns>如果删除成功,则返回true,否则为false</returns>
  284. public virtual bool DeleteSubKey()
  285. {
  286. ///返回删除是否成功
  287. bool result = false;
  288. ///判断注册表项名称是否为空,如果为空,返回false
  289. if (_subkey == string.Empty || _subkey == null)
  290. {
  291. return false;
  292. }
  293. ///创建基于注册表基项的节点
  294. RegistryKey key = GetRegDomain(_domain);
  295. if (IsSubKeyExist())
  296. {
  297. try
  298. {
  299. ///删除注册表项
  300. key.DeleteSubKey(_subkey);
  301. result = true;
  302. }
  303. catch
  304. {
  305. result = false;
  306. }
  307. }
  308. ///关闭对注册表项的更改
  309. key.Close();
  310. return result;
  311. }
  312. /// <summary>
  313. /// 删除注册表项(请先设置SubKey 属性)
  314. /// 虚方法,子类可进行重写
  315. /// </summary>
  316. /// <param name="subKey">注册表项名称</param>
  317. /// <returns>如果删除成功,则返回true,否则为false</returns>
  318. public virtual bool DeleteSubKey(string subKey)
  319. {
  320. ///返回删除是否成功
  321. bool result = false;
  322. ///判断注册表项名称是否为空,如果为空,返回false
  323. if (subKey == string.Empty || subKey == null)
  324. {
  325. return false;
  326. }
  327. ///创建基于注册表基项的节点
  328. RegistryKey key = GetRegDomain(_domain);
  329. if (IsSubKeyExist())
  330. {
  331. try
  332. {
  333. ///删除注册表项
  334. key.DeleteSubKey(subKey);
  335. result = true;
  336. }
  337. catch
  338. {
  339. result = false;
  340. }
  341. }
  342. ///关闭对注册表项的更改
  343. key.Close();
  344. return result;
  345. }
  346. /// <summary>
  347. /// 删除注册表项
  348. /// 虚方法,子类可进行重写
  349. /// </summary>
  350. /// <param name="subKey">注册表项名称</param>
  351. /// <param name="regDomain">注册表基项域</param>
  352. /// <returns>如果删除成功,则返回true,否则为false</returns>
  353. public virtual bool DeleteSubKey(string subKey, RegDomain regDomain)
  354. {
  355. ///返回删除是否成功
  356. bool result = false;
  357. ///判断注册表项名称是否为空,如果为空,返回false
  358. if (subKey == string.Empty || subKey == null)
  359. {
  360. return false;
  361. }
  362. ///创建基于注册表基项的节点
  363. RegistryKey key = GetRegDomain(regDomain);
  364. if (IsSubKeyExist(subKey, regDomain))
  365. {
  366. try
  367. {
  368. ///删除注册表项
  369. key.DeleteSubKey(subKey);
  370. result = true;
  371. }
  372. catch
  373. {
  374. result = false;
  375. }
  376. }
  377. ///关闭对注册表项的更改
  378. key.Close();
  379. return result;
  380. }
  381. #endregion
  382. #region 判断键值是否存在
  383. /// <summary>
  384. /// 判断键值是否存在(请先设置SubKey 和RegeditKey 属性)
  385. /// 虚方法,子类可进行重写
  386. /// .如果RegeditKey 为空、null,则返回false
  387. /// .如果SubKey 为空、null 或者SubKey 指定的注册表项不存在,返回false
  388. /// </summary>
  389. /// <returns>返回键值是否存在,存在返回true,否则返回false</returns>
  390. public virtual bool IsRegeditKeyExist()
  391. {
  392. ///返回结果
  393. bool result = false;
  394. ///判断是否设置键值属性
  395. if (_regeditkey == string.Empty || _regeditkey == null)
  396. {
  397. return false;
  398. }
  399. ///判断注册表项是否存在
  400. if (IsSubKeyExist())
  401. {
  402. ///打开注册表项
  403. RegistryKey key = OpenSubKey();
  404. ///键值集合
  405. string[] regeditKeyNames;
  406. ///获取键值集合
  407. regeditKeyNames = key.GetValueNames();
  408. ///遍历键值集合,如果存在键值,则退出遍历
  409. foreach (string regeditKey in regeditKeyNames)
  410. {
  411. if (string.Compare(regeditKey, _regeditkey, true) == 0)
  412. {
  413. result = true;
  414. break;
  415. }
  416. }
  417. ///关闭对注册表项的更改
  418. key.Close();
  419. }
  420. return result;
  421. }
  422. /// <summary>
  423. /// 判断键值是否存在(请先设置SubKey 属性)
  424. /// 虚方法,子类可进行重写
  425. /// 如果SubKey 为空、null 或者SubKey 指定的注册表项不存在,返回false
  426. /// </summary>
  427. /// <param name="name">键值名称</param>
  428. /// <returns>返回键值是否存在,存在返回true,否则返回false</returns>
  429. public virtual bool IsRegeditKeyExist(string name)
  430. {
  431. ///返回结果
  432. bool result = false;
  433. ///判断是否设置键值属性
  434. if (name == string.Empty || name == null)
  435. {
  436. return false;
  437. }
  438. ///判断注册表项是否存在
  439. if (IsSubKeyExist())
  440. {
  441. ///打开注册表项
  442. RegistryKey key = OpenSubKey();
  443. ///键值集合
  444. string[] regeditKeyNames;
  445. ///获取键值集合
  446. regeditKeyNames = key.GetValueNames();
  447. ///遍历键值集合,如果存在键值,则退出遍历
  448. foreach (string regeditKey in regeditKeyNames)
  449. {
  450. if (string.Compare(regeditKey, name, true) == 0)
  451. {
  452. result = true;
  453. break;
  454. }
  455. }
  456. ///关闭对注册表项的更改
  457. key.Close();
  458. }
  459. return result;
  460. }
  461. /// <summary>
  462. /// 判断键值是否存在
  463. /// 虚方法,子类可进行重写
  464. /// </summary>
  465. /// <param name="name">键值名称</param>
  466. /// <param name="subKey">注册表项名称</param>
  467. /// <returns>返回键值是否存在,存在返回true,否则返回false</returns>
  468. public virtual bool IsRegeditKeyExist(string name, string subKey)
  469. {
  470. ///返回结果
  471. bool result = false;
  472. ///判断是否设置键值属性
  473. if (name == string.Empty || name == null)
  474. {
  475. return false;
  476. }
  477. ///判断注册表项是否存在
  478. if (IsSubKeyExist())
  479. {
  480. ///打开注册表项
  481. RegistryKey key = OpenSubKey(subKey);
  482. ///键值集合
  483. string[] regeditKeyNames;
  484. ///获取键值集合
  485. regeditKeyNames = key.GetValueNames();
  486. ///遍历键值集合,如果存在键值,则退出遍历
  487. foreach (string regeditKey in regeditKeyNames)
  488. {
  489. if (string.Compare(regeditKey, name, true) == 0)
  490. {
  491. result = true;
  492. break;
  493. }
  494. }
  495. ///关闭对注册表项的更改
  496. key.Close();
  497. }
  498. return result;
  499. }
  500. /// <summary>
  501. /// 判断键值是否存在
  502. /// 虚方法,子类可进行重写
  503. /// </summary>
  504. /// <param name="name">键值名称</param>
  505. /// <param name="subKey">注册表项名称</param>
  506. /// <param name="regDomain">注册表基项域</param>
  507. /// <returns>返回键值是否存在,存在返回true,否则返回false</returns>
  508. public virtual bool IsRegeditKeyExist(string name, string subKey, RegDomain regDomain)
  509. {
  510. ///返回结果
  511. bool result = false;
  512. ///判断是否设置键值属性
  513. if (name == string.Empty || name == null)
  514. {
  515. return false;
  516. }
  517. ///判断注册表项是否存在
  518. if (IsSubKeyExist())
  519. {
  520. ///打开注册表项
  521. RegistryKey key = OpenSubKey(subKey, regDomain);
  522. ///键值集合
  523. string[] regeditKeyNames;
  524. ///获取键值集合
  525. regeditKeyNames = key.GetValueNames();
  526. ///遍历键值集合,如果存在键值,则退出遍历
  527. foreach (string regeditKey in regeditKeyNames)
  528. {
  529. if (string.Compare(regeditKey, name, true) == 0)
  530. {
  531. result = true;
  532. break;
  533. }
  534. }
  535. ///关闭对注册表项的更改
  536. key.Close();
  537. }
  538. return result;
  539. }
  540. #endregion
  541. #region 设置键值内容
  542. /// <summary>
  543. /// 设置指定的键值内容,不指定内容数据类型(请先设置RegeditKey 和SubKey 属性)
  544. /// 存在改键值则修改键值内容,不存在键值则先创建键值,再设置键值内容
  545. /// </summary>
  546. /// <param name="content">键值内容</param>
  547. /// <returns>键值内容设置成功,则返回true,否则返回false</returns>
  548. public virtual bool WriteRegeditKey(object content)
  549. {
  550. ///返回结果
  551. bool result = false;
  552. ///判断是否设置键值属性
  553. if (_regeditkey == string.Empty || _regeditkey == null)
  554. {
  555. _regeditkey = "";
  556. }
  557. ///判断注册表项是否存在,如果不存在,则直接创建
  558. if (!IsSubKeyExist(_subkey))
  559. {
  560. CreateSubKey(_subkey);
  561. }
  562. ///以可写方式打开注册表项
  563. RegistryKey key = OpenSubKey(true);
  564. ///如果注册表项打开失败,则返回false
  565. if (key == null)
  566. {
  567. return false;
  568. }
  569. try
  570. {
  571. key.SetValue(_regeditkey, content);
  572. result = true;
  573. }
  574. catch
  575. {
  576. result = false;
  577. }
  578. finally
  579. {
  580. ///关闭对注册表项的更改
  581. key.Close();
  582. }
  583. return result;
  584. }
  585. /// <summary>
  586. /// 设置指定的键值内容,不指定内容数据类型(请先设置SubKey 属性)
  587. /// 存在改键值则修改键值内容,不存在键值则先创建键值,再设置键值内容
  588. /// </summary>
  589. /// <param name="name">键值名称</param>
  590. /// <param name="content">键值内容</param>
  591. /// <returns>键值内容设置成功,则返回true,否则返回false</returns>
  592. public virtual bool WriteRegeditKey(string name, object content)
  593. {
  594. ///返回结果
  595. bool result = false;
  596. ///判断键值是否存在
  597. if (name == string.Empty || name == null)
  598. {
  599. name = "";
  600. }
  601. ///判断注册表项是否存在,如果不存在,则直接创建
  602. if (!IsSubKeyExist(_subkey))
  603. {
  604. CreateSubKey(_subkey);
  605. }
  606. ///以可写方式打开注册表项
  607. RegistryKey key = OpenSubKey(true);
  608. ///如果注册表项打开失败,则返回false
  609. if (key == null)
  610. {
  611. return false;
  612. }
  613. try
  614. {
  615. key.SetValue(name, content);
  616. result = true;
  617. }
  618. catch (Exception ex)
  619. {
  620. result = false;
  621. }
  622. finally
  623. {
  624. ///关闭对注册表项的更改
  625. key.Close();
  626. }
  627. return result;
  628. }
  629. /// <summary>
  630. /// 设置指定的键值内容,指定内容数据类型(请先设置SubKey 属性)
  631. /// 存在改键值则修改键值内容,不存在键值则先创建键值,再设置键值内容
  632. /// </summary>
  633. /// <param name="name">键值名称</param>
  634. /// <param name="content">键值内容</param>
  635. /// <returns>键值内容设置成功,则返回true,否则返回false</returns>
  636. public virtual bool WriteRegeditKey(string name, object content, RegValueKind regValueKind)
  637. {
  638. ///返回结果
  639. bool result = false;
  640. ///判断键值是否存在
  641. if (name == string.Empty || name == null)
  642. {
  643. name = "";
  644. }
  645. ///判断注册表项是否存在,如果不存在,则直接创建
  646. if (!IsSubKeyExist(_subkey))
  647. {
  648. CreateSubKey(_subkey);
  649. }
  650. ///以可写方式打开注册表项
  651. RegistryKey key = OpenSubKey(true);
  652. ///如果注册表项打开失败,则返回false
  653. if (key == null)
  654. {
  655. return false;
  656. }
  657. try
  658. {
  659. key.SetValue(name, content, GetRegValueKind(regValueKind));
  660. result = true;
  661. }
  662. catch
  663. {
  664. result = false;
  665. }
  666. finally
  667. {
  668. ///关闭对注册表项的更改
  669. key.Close();
  670. }
  671. return result;
  672. }
  673. #endregion
  674. #region 读取键值内容
  675. /// <summary>
  676. /// 读取键值内容(请先设置RegeditKey 和SubKey 属性)
  677. /// .如果RegeditKey 为空、null 或者RegeditKey 指示的键值不存在,返回null
  678. /// .如果SubKey 为空、null 或者SubKey 指示的注册表项不存在,返回null
  679. /// .反之,则返回键值内容
  680. /// </summary>
  681. /// <returns>返回键值内容</returns>
  682. public virtual object ReadRegeditKey()
  683. {
  684. ///键值内容结果
  685. object obj = null;
  686. ///判断是否设置键值属性
  687. if (_regeditkey == string.Empty || _regeditkey == null)
  688. {
  689. return null;
  690. }
  691. ///判断键值是否存在
  692. if (IsRegeditKeyExist(_regeditkey))
  693. {
  694. ///打开注册表项
  695. RegistryKey key = OpenSubKey();
  696. if (key != null)
  697. {
  698. obj = key.GetValue(_regeditkey);
  699. }
  700. ///关闭对注册表项的更改
  701. key.Close();
  702. }
  703. return obj;
  704. }
  705. /// <summary>
  706. /// 读取键值内容(请先设置SubKey 属性)
  707. /// .如果SubKey 为空、null 或者SubKey 指示的注册表项不存在,返回null
  708. /// .反之,则返回键值内容
  709. /// </summary>
  710. /// <param name="name">键值名称</param>
  711. /// <returns>返回键值内容</returns>
  712. public virtual object ReadRegeditKey(string name)
  713. {
  714. ///键值内容结果
  715. object obj = null;
  716. ///判断是否设置键值属性
  717. if (name == string.Empty || name == null)
  718. {
  719. return null;
  720. }
  721. ///判断键值是否存在
  722. if (IsRegeditKeyExist(name))
  723. {
  724. ///打开注册表项
  725. RegistryKey key = OpenSubKey();
  726. if (key != null)
  727. {
  728. obj = key.GetValue(name);
  729. }
  730. ///关闭对注册表项的更改
  731. key.Close();
  732. }
  733. return obj;
  734. }
  735. /// <summary>
  736. /// 读取键值内容
  737. /// </summary>
  738. /// <param name="name">键值名称</param>
  739. /// <param name="subKey">注册表项名称</param>
  740. /// <returns>返回键值内容</returns>
  741. public virtual object ReadRegeditKey(string name, string subKey)
  742. {
  743. ///键值内容结果
  744. object obj = null;
  745. ///判断是否设置键值属性
  746. if (name == string.Empty || name == null)
  747. {
  748. return null;
  749. }
  750. ///判断键值是否存在
  751. if (IsRegeditKeyExist(name))
  752. {
  753. ///打开注册表项
  754. RegistryKey key = OpenSubKey(subKey);
  755. if (key != null)
  756. {
  757. obj = key.GetValue(name);
  758. }
  759. ///关闭对注册表项的更改
  760. key.Close();
  761. }
  762. return obj;
  763. }
  764. /// <summary>
  765. /// 读取键值内容
  766. /// </summary>
  767. /// <param name="name">键值名称</param>
  768. /// <param name="subKey">注册表项名称</param>
  769. /// <param name="regDomain">注册表基项域</param>
  770. /// <returns>返回键值内容</returns>
  771. public virtual object ReadRegeditKey(string name, string subKey, RegDomain regDomain)
  772. {
  773. ///键值内容结果
  774. object obj = null;
  775. ///判断是否设置键值属性
  776. if (name == string.Empty || name == null)
  777. {
  778. return null;
  779. }
  780. ///判断键值是否存在
  781. if (IsRegeditKeyExist(name))
  782. {
  783. ///打开注册表项
  784. RegistryKey key = OpenSubKey(subKey, regDomain);
  785. if (key != null)
  786. {
  787. obj = key.GetValue(name);
  788. }
  789. ///关闭对注册表项的更改
  790. key.Close();
  791. }
  792. return obj;
  793. }
  794. #endregion
  795. #region 删除键值
  796. /// <summary>
  797. /// 删除键值(请先设置RegeditKey 和SubKey 属性)
  798. /// .如果RegeditKey 为空、null 或者RegeditKey 指示的键值不存在,返回false
  799. /// .如果SubKey 为空、null 或者SubKey 指示的注册表项不存在,返回false
  800. /// </summary>
  801. /// <returns>如果删除成功,返回true,否则返回false</returns>
  802. public virtual bool DeleteRegeditKey()
  803. {
  804. ///删除结果
  805. bool result = false;
  806. ///判断是否设置键值属性,如果没有设置,则返回false
  807. if (_regeditkey == string.Empty || _regeditkey == null)
  808. {
  809. return false;
  810. }
  811. ///判断键值是否存在
  812. if (IsRegeditKeyExist(_regeditkey))
  813. {
  814. ///以可写方式打开注册表项
  815. RegistryKey key = OpenSubKey(true);
  816. if (key != null)
  817. {
  818. try
  819. {
  820. ///删除键值
  821. key.DeleteValue(_regeditkey);
  822. result = true;
  823. }
  824. catch
  825. {
  826. result = false;
  827. }
  828. finally
  829. {
  830. ///关闭对注册表项的更改
  831. key.Close();
  832. }
  833. }
  834. }
  835. return result;
  836. }
  837. /// <summary>
  838. /// 删除键值(请先设置SubKey 属性)
  839. /// 如果SubKey 为空、null 或者SubKey 指示的注册表项不存在,返回false
  840. /// </summary>
  841. /// <param name="name">键值名称</param>
  842. /// <returns>如果删除成功,返回true,否则返回false</returns>
  843. public virtual bool DeleteRegeditKey(string name)
  844. {
  845. ///删除结果
  846. bool result = false;
  847. ///判断键值名称是否为空,如果为空,则返回false
  848. if (name == string.Empty || name == null)
  849. {
  850. return false;
  851. }
  852. ///判断键值是否存在
  853. if (IsRegeditKeyExist(name))
  854. {
  855. ///以可写方式打开注册表项
  856. RegistryKey key = OpenSubKey(true);
  857. if (key != null)
  858. {
  859. try
  860. {
  861. ///删除键值
  862. key.DeleteValue(name);
  863. result = true;
  864. }
  865. catch
  866. {
  867. result = false;
  868. }
  869. finally
  870. {
  871. ///关闭对注册表项的更改
  872. key.Close();
  873. }
  874. }
  875. }
  876. return result;
  877. }
  878. /// <summary>
  879. /// 删除键值
  880. /// </summary>
  881. /// <param name="name">键值名称</param>
  882. /// <param name="subKey">注册表项名称</param>
  883. /// <returns>如果删除成功,返回true,否则返回false</returns>
  884. public virtual bool DeleteRegeditKey(string name, string subKey)
  885. {
  886. ///删除结果
  887. bool result = false;
  888. ///判断键值名称和注册表项名称是否为空,如果为空,则返回false
  889. if (name == string.Empty || name == null || subKey == string.Empty || subKey == null)
  890. {
  891. return false;
  892. }
  893. ///判断键值是否存在
  894. if (IsRegeditKeyExist(name))
  895. {
  896. ///以可写方式打开注册表项
  897. RegistryKey key = OpenSubKey(subKey, true);
  898. if (key != null)
  899. {
  900. try
  901. {
  902. ///删除键值
  903. key.DeleteValue(name);
  904. result = true;
  905. }
  906. catch
  907. {
  908. result = false;
  909. }
  910. finally
  911. {
  912. ///关闭对注册表项的更改
  913. key.Close();
  914. }
  915. }
  916. }
  917. return result;
  918. }
  919. /// <summary>
  920. /// 删除键值
  921. /// </summary>
  922. /// <param name="name">键值名称</param>
  923. /// <param name="subKey">注册表项名称</param>
  924. /// <param name="regDomain">注册表基项域</param>
  925. /// <returns>如果删除成功,返回true,否则返回false</returns>
  926. public virtual bool DeleteRegeditKey(string name, string subKey, RegDomain regDomain)
  927. {
  928. ///删除结果
  929. bool result = false;
  930. ///判断键值名称和注册表项名称是否为空,如果为空,则返回false
  931. if (name == string.Empty || name == null || subKey == string.Empty || subKey == null)
  932. {
  933. return false;
  934. }
  935. ///判断键值是否存在
  936. if (IsRegeditKeyExist(name))
  937. {
  938. ///以可写方式打开注册表项
  939. RegistryKey key = OpenSubKey(subKey, regDomain, true);
  940. if (key != null)
  941. {
  942. try
  943. {
  944. ///删除键值
  945. key.DeleteValue(name);
  946. result = true;
  947. }
  948. catch
  949. {
  950. result = false;
  951. }
  952. finally
  953. {
  954. ///关闭对注册表项的更改
  955. key.Close();
  956. }
  957. }
  958. }
  959. return result;
  960. }
  961. #endregion
  962. #endregion
  963. #region 受保护方法
  964. /// <summary>
  965. /// 获取注册表基项域对应顶级节点
  966. /// 例子:如regDomain 是ClassesRoot,则返回Registry.ClassesRoot
  967. /// </summary>
  968. /// <param name="regDomain">注册表基项域</param>
  969. /// <returns>注册表基项域对应顶级节点</returns>
  970. protected RegistryKey GetRegDomain(RegDomain regDomain)
  971. {
  972. ///创建基于注册表基项的节点
  973. RegistryKey key;
  974. #region 判断注册表基项域
  975. switch (regDomain)
  976. {
  977. case RegDomain.ClassesRoot:
  978. key = Registry.ClassesRoot; break;
  979. case RegDomain.CurrentUser:
  980. key = Registry.CurrentUser; break;
  981. case RegDomain.LocalMachine:
  982. key = Registry.LocalMachine; break;
  983. case RegDomain.User:
  984. key = Registry.Users; break;
  985. case RegDomain.CurrentConfig:
  986. key = Registry.CurrentConfig; break;
  987. case RegDomain.DynDa:
  988. key = Registry.DynData; break;
  989. case RegDomain.PerformanceData:
  990. key = Registry.PerformanceData; break;
  991. default:
  992. key = Registry.LocalMachine; break;
  993. }
  994. #endregion
  995. return key;
  996. }
  997. /// <summary>
  998. /// 获取在注册表中对应的值数据类型
  999. /// 例子:如regValueKind 是DWord,则返回RegistryValueKind.DWord
  1000. /// </summary>
  1001. /// <param name="regValueKind">注册表数据类型</param>
  1002. /// <returns>注册表中对应的数据类型</returns>
  1003. protected RegistryValueKind GetRegValueKind(RegValueKind regValueKind)
  1004. {
  1005. RegistryValueKind regValueK;
  1006. #region 判断注册表数据类型
  1007. switch (regValueKind)
  1008. {
  1009. case RegValueKind.Unknown:
  1010. regValueK = RegistryValueKind.Unknown; break;
  1011. case RegValueKind.String:
  1012. regValueK = RegistryValueKind.String; break;
  1013. case RegValueKind.ExpandString:
  1014. regValueK = RegistryValueKind.ExpandString; break;
  1015. case RegValueKind.Binary:
  1016. regValueK = RegistryValueKind.Binary; break;
  1017. case RegValueKind.DWord:
  1018. regValueK = RegistryValueKind.DWord; break;
  1019. case RegValueKind.MultiString:
  1020. regValueK = RegistryValueKind.MultiString; break;
  1021. case RegValueKind.QWord:
  1022. regValueK = RegistryValueKind.QWord; break;
  1023. default:
  1024. regValueK = RegistryValueKind.String; break;
  1025. }
  1026. #endregion
  1027. return regValueK;
  1028. }
  1029. #region 打开注册表项
  1030. /// <summary>
  1031. /// 打开注册表项节点,以只读方式检索子项
  1032. /// 虚方法,子类可进行重写
  1033. /// </summary>
  1034. /// <returns>如果SubKey为空、null或者SubKey指示注册表项不存在,则返回null,否则返回注册表节点</returns>
  1035. protected virtual RegistryKey OpenSubKey()
  1036. {
  1037. ///判断注册表项名称是否为空
  1038. if (_subkey == string.Empty || _subkey == null)
  1039. {
  1040. return null;
  1041. }
  1042. ///创建基于注册表基项的节点
  1043. RegistryKey key = GetRegDomain(_domain);
  1044. ///要打开的注册表项的节点
  1045. RegistryKey sKey = null;
  1046. ///打开注册表项
  1047. sKey = key.OpenSubKey(_subkey);
  1048. ///关闭对注册表项的更改
  1049. key.Close();
  1050. ///返回注册表节点
  1051. return sKey;
  1052. }
  1053. /// <summary>
  1054. /// 打开注册表项节点
  1055. /// 虚方法,子类可进行重写
  1056. /// </summary>
  1057. /// <param name="writable">如果需要项的写访问权限,则设置为 true</param>
  1058. /// <returns>如果SubKey为空、null或者SubKey指示注册表项不存在,则返回null,否则返回注册表节点</returns>
  1059. protected virtual RegistryKey OpenSubKey(bool writable)
  1060. {
  1061. ///判断注册表项名称是否为空
  1062. if (_subkey == string.Empty || _subkey == null)
  1063. {
  1064. return null;
  1065. }
  1066. ///创建基于注册表基项的节点
  1067. RegistryKey key = GetRegDomain(_domain);
  1068. ///要打开的注册表项的节点
  1069. RegistryKey sKey = null;
  1070. ///打开注册表项
  1071. sKey = key.OpenSubKey(_subkey, writable);
  1072. ///关闭对注册表项的更改
  1073. key.Close();
  1074. ///返回注册表节点
  1075. return sKey;
  1076. }
  1077. /// <summary>
  1078. /// 打开注册表项节点,以只读方式检索子项
  1079. /// 虚方法,子类可进行重写
  1080. /// </summary>
  1081. /// <param name="subKey">注册表项名称</param>
  1082. /// <returns>如果SubKey为空、null或者SubKey指示注册表项不存在,则返回null,否则返回注册表节点</returns>
  1083. protected virtual RegistryKey OpenSubKey(string subKey)
  1084. {
  1085. ///判断注册表项名称是否为空
  1086. if (subKey == string.Empty || subKey == null)
  1087. {
  1088. return null;
  1089. }
  1090. ///创建基于注册表基项的节点
  1091. RegistryKey key = GetRegDomain(_domain);
  1092. ///要打开的注册表项的节点
  1093. RegistryKey sKey = null;
  1094. ///打开注册表项
  1095. sKey = key.OpenSubKey(subKey);
  1096. ///关闭对注册表项的更改
  1097. key.Close();
  1098. ///返回注册表节点
  1099. return sKey;
  1100. }
  1101. /// <summary>
  1102. /// 打开注册表项节点,以只读方式检索子项
  1103. /// 虚方法,子类可进行重写
  1104. /// </summary>
  1105. /// <param name="subKey">注册表项名称</param>
  1106. /// <param name="writable">如果需要项的写访问权限,则设置为 true</param>
  1107. /// <returns>如果SubKey为空、null或者SubKey指示注册表项不存在,则返回null,否则返回注册表节点</returns>
  1108. protected virtual RegistryKey OpenSubKey(string subKey, bool writable)
  1109. {
  1110. ///判断注册表项名称是否为空
  1111. if (subKey == string.Empty || subKey == null)
  1112. {
  1113. return null;
  1114. }
  1115. ///创建基于注册表基项的节点
  1116. RegistryKey key = GetRegDomain(_domain);
  1117. ///要打开的注册表项的节点
  1118. RegistryKey sKey = null;
  1119. ///打开注册表项
  1120. sKey = key.OpenSubKey(subKey, writable);
  1121. ///关闭对注册表项的更改
  1122. key.Close();
  1123. ///返回注册表节点
  1124. return sKey;
  1125. }
  1126. /// <summary>
  1127. /// 打开注册表项节点,以只读方式检索子项
  1128. /// 虚方法,子类可进行重写
  1129. /// </summary>
  1130. /// <param name="subKey">注册表项名称</param>
  1131. /// <param name="regDomain">注册表基项域</param>
  1132. /// <returns>如果SubKey为空、null或者SubKey指示注册表项不存在,则返回null,否则返回注册表节点</returns>
  1133. protected virtual RegistryKey OpenSubKey(string subKey, RegDomain regDomain)
  1134. {
  1135. ///判断注册表项名称是否为空
  1136. if (subKey == string.Empty || subKey == null)
  1137. {
  1138. return null;
  1139. }
  1140. ///创建基于注册表基项的节点
  1141. RegistryKey key = GetRegDomain(regDomain);
  1142. ///要打开的注册表项的节点
  1143. RegistryKey sKey = null;
  1144. ///打开注册表项
  1145. sKey = key.OpenSubKey(subKey);
  1146. ///关闭对注册表项的更改
  1147. key.Close();
  1148. ///返回注册表节点
  1149. return sKey;
  1150. }
  1151. /// <summary>
  1152. /// 打开注册表项节点
  1153. /// 虚方法,子类可进行重写
  1154. /// </summary>
  1155. /// <param name="subKey">注册表项名称</param>
  1156. /// <param name="regDomain">注册表基项域</param>
  1157. /// <param name="writable">如果需要项的写访问权限,则设置为 true</param>
  1158. /// <returns>如果SubKey为空、null或者SubKey指示注册表项不存在,则返回null,否则返回注册表节点</returns>
  1159. protected virtual RegistryKey OpenSubKey(string subKey, RegDomain regDomain, bool writable)
  1160. {
  1161. ///判断注册表项名称是否为空
  1162. if (subKey == string.Empty || subKey == null)
  1163. {
  1164. return null;
  1165. }
  1166. ///创建基于注册表基项的节点
  1167. RegistryKey key = GetRegDomain(regDomain);
  1168. ///要打开的注册表项的节点
  1169. RegistryKey sKey = null;
  1170. ///打开注册表项
  1171. sKey = key.OpenSubKey(subKey, writable);
  1172. ///关闭对注册表项的更改
  1173. key.Close();
  1174. ///返回注册表节点
  1175. return sKey;
  1176. }
  1177. #endregion
  1178. #endregion
  1179. }
  1180. /// <summary>
  1181. /// 注册表基项静态域
  1182. ///1.Registry.ClassesRoot 对应于HKEY_CLASSES_ROOT 主键
  1183. ///2.Registry.CurrentUser 对应于HKEY_CURRENT_USER 主键
  1184. ///3.Registry.LocalMachine 对应于 HKEY_LOCAL_MACHINE 主键
  1185. ///4.Registry.User 对应于 HKEY_USER 主键
  1186. ///5.Registry.CurrentConfig 对应于HEKY_CURRENT_CONFIG 主键
  1187. ///6.Registry.DynDa 对应于HKEY_DYN_DATA 主键
  1188. ///7.Registry.PerformanceData 对应于HKEY_PERFORMANCE_DATA 主键
  1189. /// </summary>
  1190. public enum RegDomain
  1191. {
  1192. /// <summary>
  1193. /// 对应于HKEY_CLASSES_ROOT 主键
  1194. /// </summary>
  1195. ClassesRoot = 0,
  1196. /// <summary>
  1197. /// 对应于HKEY_CURRENT_USER 主键
  1198. /// </summary>
  1199. CurrentUser = 1,
  1200. /// <summary>
  1201. /// 对应于 HKEY_LOCAL_MACHINE 主键
  1202. /// </summary>
  1203. LocalMachine = 2,
  1204. /// <summary>
  1205. /// 对应于 HKEY_USER 主键
  1206. /// </summary>
  1207. User = 3,
  1208. /// <summary>
  1209. /// 对应于HEKY_CURRENT_CONFIG 主键
  1210. /// </summary>
  1211. CurrentConfig = 4,
  1212. /// <summary>
  1213. /// 对应于HKEY_DYN_DATA 主键
  1214. /// </summary>
  1215. DynDa = 5,
  1216. /// <summary>
  1217. /// 对应于HKEY_PERFORMANCE_DATA 主键
  1218. /// </summary>
  1219. PerformanceData = 6
  1220. }
  1221. /// <summary>
  1222. /// 指定在注册表中存储值时所用的数据类型,或标识注册表中某个值的数据类型
  1223. ///1.RegistryValueKind.Unknown
  1224. ///2.RegistryValueKind.String
  1225. ///3.RegistryValueKind.ExpandString
  1226. ///4.RegistryValueKind.Binary
  1227. ///5.RegistryValueKind.DWord
  1228. ///6.RegistryValueKind.MultiString
  1229. ///7.RegistryValueKind.QWord
  1230. /// </summary>
  1231. public enum RegValueKind
  1232. {
  1233. /// <summary>
  1234. /// 指示一个不受支持的注册表数据类型。例如,不支持 Microsoft Win API 注册表数据类型 REG_RESOURCE_LIST。使用此值指定
  1235. /// </summary>
  1236. Unknown = 0,
  1237. /// <summary>
  1238. /// 指定一个以 Null 结尾的字符串。此值与 Win API 注册表数据类型 REG_SZ 等效。
  1239. /// </summary>
  1240. String = 1,
  1241. /// <summary>
  1242. /// 指定一个以 NULL 结尾的字符串,该字符串中包含对环境变量(如 %PATH%,当值被检索时,就会展开)的未展开的引用。
  1243. /// 此值与 Win API 注册表数据类型 REG_EXPAND_SZ 等效。
  1244. /// </summary>
  1245. ExpandString = 2,
  1246. /// <summary>
  1247. /// 指定任意格式的二进制数据。此值与 Win API 注册表数据类型 REG_BINARY 等效。
  1248. /// </summary>
  1249. Binary = 3,
  1250. /// <summary>
  1251. /// 指定一个 位二进制数。此值与 Win API 注册表数据类型 REG_DWORD 等效。
  1252. /// </summary>
  1253. DWord = 4,
  1254. /// <summary>
  1255. /// 指定一个以 NULL 结尾的字符串数组,以两个空字符结束。此值与 WinAPI 注册表数据类型 REG_MULTI_SZ 等效。
  1256. /// </summary>
  1257. MultiString = 5,
  1258. /// <summary>
  1259. /// 指定一个 位二进制数。此值与 Win API 注册表数据类型 REG_QWORD 等效。
  1260. /// </summary>
  1261. QWord = 6
  1262. }
  1263. }