星火直播PC
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RegistryHelper.cs 48KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  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. return false;
  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. return false;
  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. return false;
  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. }