using Microsoft.Win32; using System; using System.Collections.Specialized; using System.Drawing; using System.Drawing.Imaging; using System.IO; namespace PaintDotNet.SystemLayer { /// /// Stores non-volatile name/value settings. These persist between sessions of the application. /// /// /// On Windows, this class uses the registry. /// public sealed class Settings : ISimpleCollection { private const string hkcuKey = @"SOFTWARE\Metis Vision"; public static readonly Settings SystemWide = new Settings(Microsoft.Win32.Registry.LocalMachine); public static readonly Settings CurrentUser = new Settings(Microsoft.Win32.Registry.CurrentUser); private const string pointXSuffix = ".X"; private const string pointYSuffix = ".Y"; private RegistryKey rootKey; private Settings(RegistryKey rootKey) { this.rootKey = rootKey; } private RegistryKey CreateSettingsKey(bool writable) { RegistryKey softwareKey = null; try { softwareKey = this.rootKey.OpenSubKey(hkcuKey, writable); } catch (Exception) { softwareKey = null; } if (softwareKey == null) { try { softwareKey = rootKey.CreateSubKey(hkcuKey); } catch (Exception) { throw; } } return softwareKey; } public bool TryDelete(string key) { try { Delete(key); return true; } catch (Exception) { return false; } } /// /// Deletes a settings key. /// /// The key to delete. public void Delete(string key) { using (RegistryKey pdnKey = CreateSettingsKey(true)) { pdnKey.DeleteValue(key, false); } } /// /// Deletes several settings keys. /// /// The keys to delete. public void Delete(string[] keys) { using (RegistryKey pdnKey = CreateSettingsKey(true)) { foreach (string key in keys) { pdnKey.DeleteValue(key, false); } } } /// /// Retrieves the value of a settings key. /// /// The name of the key to retrieve. /// The value of the key. public object GetObject(string key) { using (RegistryKey pdnKey = CreateSettingsKey(false)) { return pdnKey.GetValue(key); } } /// /// Retrieves the value of a settings key. /// /// The name of the key to retrieve. /// The default value to use if the key doesn't exist. /// The value of the key, or defaultValue if it didn't exist. public object GetObject(string key, object defaultValue) { try { using (RegistryKey pdnKey = CreateSettingsKey(false)) { return pdnKey.GetValue(key, defaultValue); } } catch (Exception) { return defaultValue; } } /// /// Sets the value of a settings key. /// /// The name of the key to set. /// The new value of the key. public void SetObject(string key, object value) { using (RegistryKey pdnKey = CreateSettingsKey(true)) { pdnKey.SetValue(key, value); } } /// /// Retrieves the value of a settings key. /// /// The name of the key to retrieve. /// The value of the key. public string GetString(string key) { return (string)GetObject(key); } /// /// Retrieves the value of a settings key. /// /// The name of the key to retrieve. /// The default value to use if the key doesn't exist. /// The value of the key, or defaultValue if it didn't exist. public string GetString(string key, string defaultValue) { return (string)GetObject(key, defaultValue); } /// /// Sets the value of a settings key. /// /// The name of the key to set. /// The new value of the key. public void SetString(string key, string value) { SetObject(key, value); } /// /// Saves the given strings. /// public void SetStrings(NameValueCollection nvc) { foreach (string key in nvc.Keys) { string value = nvc[key]; SetString("Test\\" + key, value); } } /// /// Retrieves the value of a settings key. /// /// The name of the key to retrieve. /// The value of the key. public bool GetBoolean(string key) { return bool.Parse(GetString(key)); } /// /// Retrieves the value of a settings key. /// /// The name of the key to retrieve. /// The default value to use if the key doesn't exist. /// The value of the key, or defaultValue if it didn't exist. public bool GetBoolean(string key, bool defaultValue) { return bool.Parse(GetString(key, defaultValue.ToString())); } /// /// Sets the value of a settings key. /// /// The name of the key to set. /// The new value of the key. public void SetBoolean(string key, bool value) { SetString(key, value.ToString()); } /// /// Retrieves the value of a settings key. /// /// The name of the key to retrieve. /// The value of the key. public Point GetPoint(string key) { int x = GetInt32(key + pointXSuffix); int y = GetInt32(key + pointYSuffix); return new Point(x, y); } /// /// Retrieves the value of a settings key. /// /// The name of the key to retrieve. /// The default value to use if the key doesn't exist. /// The value of the key, or defaultValue if it didn't exist. public Point GetPoint(string key, Point defaultValue) { int x = GetInt32(key + pointXSuffix, defaultValue.X); int y = GetInt32(key + pointYSuffix, defaultValue.Y); return new Point(x, y); } /// /// Sets the value of a settings key. /// /// The name of the key to set. /// The new value of the key. public void SetPoint(string key, Point value) { SetInt32(key + pointXSuffix, value.X); SetInt32(key + pointYSuffix, value.Y); } /// /// Retrieves the value of a settings key. /// /// The name of the key to retrieve. /// The value of the key. public Int32 GetInt32(string key) { return Int32.Parse(GetString(key)); } /// /// Retrieves the value of a settings key. /// /// The name of the key to retrieve. /// The default value to use if the key doesn't exist. /// The value of the key, or defaultValue if it didn't exist. public Int32 GetInt32(string key, Int32 defaultValue) { return Int32.Parse(GetString(key, defaultValue.ToString())); } /// /// Retrieves the value of a settings key. /// /// The name of the key to retrieve. /// The default value to use if the key doesn't exist. /// The value of the key, or defaultValue if it didn't exist. public float GetSingle(string key, float defaultValue) { return Single.Parse(GetString(key, defaultValue.ToString())); } /// /// Retrieves the value of a settings key. /// /// The name of the key to retrieve. /// The value of the key. public float GetSingle(string key) { return Single.Parse(GetString(key)); } /// /// Sets the value of a settings key. /// /// The name of the key to set. /// The new value of the key. public void SetInt32(string key, int value) { SetString(key, value.ToString()); } /// /// Sets the value of a settings key. /// /// The name of the key to set. /// The new value of the key. public void SetSingle(string key, float value) { SetString(key, value.ToString()); } /// /// Gets the value of a settings key. /// /// The name of the key to retrieve. /// The value of the key. /// This method treats the key value as a stream of base64 encoded bytes that represent a PNG image. public Image GetImage(string key) { string imageB64 = GetString(key); byte[] pngBytes = Convert.FromBase64String(imageB64); MemoryStream ms = new MemoryStream(pngBytes); Image image = Image.FromStream(ms); ms.Close(); return image; } /// /// Sets the value of a settings key. /// /// The name of the key to set. /// The new value of the key. /// This method saves the key value as a stream of base64 encoded bytes that represent a PNG image. public void SetImage(string key, Image value) { MemoryStream ms = new MemoryStream(); value.Save(ms, ImageFormat.Png); byte[] buffer = ms.GetBuffer(); string base64 = Convert.ToBase64String(buffer); SetString(key, base64); ms.Close(); } public string Get(string key) { return GetString(key); } public void Set(string key, string value) { SetString(key, value); } } }