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"; /// /// Gets the value by registry key. If the key does not exist, return empty string. /// /// registry key /// Returns the value of the specified key. 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; } /// /// Saves the key and the value to registry. /// /// registry key /// the value of the key /// Returns true if successful, otherwise return false. 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; } } }