12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- using Microsoft.Win32;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ProjectBase.Controls.Others
- {
- internal sealed class RegistryHelper
- {
- private static string softwareKey = @"Software\DeepLand\OrderWater";
- /// <summary>
- /// Gets the value by registry key. If the key does not exist, return empty string.
- /// </summary>
- /// <param name="key">registry key</param>
- /// <returns>Returns the value of the specified key.</returns>
- public static string GetValue(string key)
- {
- const string parameter = "key";
- if (null == key)
- {
- throw new ArgumentNullException(parameter);
- }
- string strRet = string.Empty;
- try
- {
- RegistryKey regKey = Registry.CurrentUser.OpenSubKey(softwareKey);
- strRet = regKey.GetValue(key).ToString();
- }
- catch
- {
- strRet = "";
- }
- return strRet;
- }
- /// <summary>
- /// Saves the key and the value to registry.
- /// </summary>
- /// <param name="key">registry key</param>
- /// <param name="value">the value of the key</param>
- /// <returns>Returns true if successful, otherwise return false.</returns>
- public static bool SaveValue(string key, string value)
- {
- const string parameter1 = "key";
- const string parameter2 = "value";
- if (null == key)
- {
- throw new ArgumentNullException(parameter1);
- }
- if (null == value)
- {
- throw new ArgumentNullException(parameter2);
- }
- bool bReturn = false;
- RegistryKey reg;
- reg = Registry.CurrentUser.OpenSubKey(softwareKey, true);
- if (null == reg)
- {
- reg = Registry.CurrentUser.CreateSubKey(softwareKey);
- }
- reg.SetValue(key, value);
- return bReturn;
- }
- }
- }
|