Regedit.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Win32;
  6. namespace Aitex.UI.Charting.ViewModel
  7. {
  8. /// <summary>
  9. /// 注册表工具
  10. /// </summary>
  11. public class RegEdit
  12. {
  13. /// <summary>
  14. /// 注册表读取
  15. /// </summary>
  16. /// <param name="appName"></param>
  17. /// <param name="keyName"></param>
  18. /// <param name="keyValue"></param>
  19. /// <returns></returns>
  20. public static string ReadRegedit(string appName, string keyName)
  21. {
  22. if (string.IsNullOrEmpty(appName) || string.IsNullOrEmpty(keyName))
  23. return string.Empty;
  24. string ret = string.Empty;
  25. RegistryKey hklm = Registry.CurrentUser;
  26. RegistryKey software = hklm.OpenSubKey("Software", false);
  27. RegistryKey aimdir = software.OpenSubKey(appName);
  28. if (aimdir != null)
  29. {
  30. ret = Convert.ToString(aimdir.GetValue(keyName));
  31. aimdir.Close();
  32. }
  33. software.Close();
  34. hklm.Close();
  35. return ret;
  36. }
  37. /// <summary>
  38. /// 注册表修改
  39. /// </summary>
  40. /// <param name="appName"></param>
  41. /// <param name="keyName"></param>
  42. /// <param name="keyValue"></param>
  43. /// <returns></returns>
  44. public static bool WriteRegedit(string appName, string keyName, string keyValue)
  45. {
  46. if (string.IsNullOrEmpty(appName) || string.IsNullOrEmpty(keyName))
  47. return false;
  48. RegistryKey hklm = Registry.CurrentUser;
  49. RegistryKey software = hklm.OpenSubKey("Software", true);
  50. RegistryKey aimdir = software.CreateSubKey(appName);
  51. aimdir.SetValue(keyName, keyValue);
  52. aimdir.Close();
  53. software.Close();
  54. hklm.Close();
  55. return true;
  56. }
  57. }
  58. }