using Microsoft.Win32; using SweetLib.Classes.Exception; namespace SweetLib.Classes.Storer { /// /// Implementation of an interface which stores the data inside the registry. /// /// /// Sections will be interpreted as subkeys on registry level. /// public class RegistryStorer : IStorer { /// /// The base registry key in which will be operated. /// public RegistryKey OperatingRegistryKey { get; } /// /// Creates a new instance of with a specified application name. /// /// The applications base name. This will be used as name for a sub key inside the software key below the base key. /// /// This will use current user as the base key. /// public RegistryStorer(string appName) : this(Registry.CurrentUser, appName) { } /// /// Creates a new instance of with a specified application name. /// /// Provide a key of , e.G. Registry.CurrentUser. /// The applications base name. This will be used as name for a sub key inside the software key below the base key. public RegistryStorer(RegistryKey baseRegistryKey, string appName) { baseRegistryKey = baseRegistryKey.CreateSubKey("SOFTWARE"); OperatingRegistryKey = baseRegistryKey?.CreateSubKey(appName); if (OperatingRegistryKey == null) throw new RegistryStorerException("Unable to create registriy key."); } public string ReadString(string section, string key, string defaultValue = "") { var localRegKey = OperatingRegistryKey.OpenSubKey(section); return (string)localRegKey?.GetValue(key.ToUpper()); } public int ReadInteger(string section, string key, int defaultValue = 0) { int result; if (!int.TryParse(ReadString(section, key, defaultValue.ToString()), out result)) result = defaultValue; return result; } public bool ReadBool(string section, string key, bool defaultValue = false) { return ReadInteger(section, key, defaultValue ? 1 : 0) > 0; } public bool HasKey(string section, string key) { return ReadString(section, key).Length > 0; } public void WriteString(string section, string key, string value) { var localRegKey = OperatingRegistryKey.CreateSubKey(section); localRegKey?.SetValue(key.ToUpper(), value); } public void WriteInteger(string section, string key, int value) { WriteString(section, key, value.ToString()); } public void WriteBool(string section, string key, bool value) { WriteInteger(section, key, value ? 1 : 0); } public void DeleteKey(string section, string key) { var localRegKey = OperatingRegistryKey.CreateSubKey(section); localRegKey?.DeleteValue(key); } public void DeleteSection(string section) { OperatingRegistryKey.DeleteSubKey(section); } } }