RegistryHelper.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 ProjectBase.Controls.Others
  8. {
  9. internal sealed class RegistryHelper
  10. {
  11. private static string softwareKey = @"Software\DeepLand\OrderWater";
  12. /// <summary>
  13. /// Gets the value by registry key. If the key does not exist, return empty string.
  14. /// </summary>
  15. /// <param name="key">registry key</param>
  16. /// <returns>Returns the value of the specified key.</returns>
  17. public static string GetValue(string key)
  18. {
  19. const string parameter = "key";
  20. if (null == key)
  21. {
  22. throw new ArgumentNullException(parameter);
  23. }
  24. string strRet = string.Empty;
  25. try
  26. {
  27. RegistryKey regKey = Registry.CurrentUser.OpenSubKey(softwareKey);
  28. strRet = regKey.GetValue(key).ToString();
  29. }
  30. catch
  31. {
  32. strRet = "";
  33. }
  34. return strRet;
  35. }
  36. /// <summary>
  37. /// Saves the key and the value to registry.
  38. /// </summary>
  39. /// <param name="key">registry key</param>
  40. /// <param name="value">the value of the key</param>
  41. /// <returns>Returns true if successful, otherwise return false.</returns>
  42. public static bool SaveValue(string key, string value)
  43. {
  44. const string parameter1 = "key";
  45. const string parameter2 = "value";
  46. if (null == key)
  47. {
  48. throw new ArgumentNullException(parameter1);
  49. }
  50. if (null == value)
  51. {
  52. throw new ArgumentNullException(parameter2);
  53. }
  54. bool bReturn = false;
  55. RegistryKey reg;
  56. reg = Registry.CurrentUser.OpenSubKey(softwareKey, true);
  57. if (null == reg)
  58. {
  59. reg = Registry.CurrentUser.CreateSubKey(softwareKey);
  60. }
  61. reg.SetValue(key, value);
  62. return bReturn;
  63. }
  64. }
  65. }