123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- using Microsoft.Win32;
- using System;
- using System.Collections.Specialized;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- namespace PaintDotNet.SystemLayer
- {
- /// <summary>
- /// Stores non-volatile name/value settings. These persist between sessions of the application.
- /// </summary>
- /// <remarks>
- /// On Windows, this class uses the registry.
- /// </remarks>
- public sealed class Settings
- : ISimpleCollection<string, string>
- {
- 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;
- }
- }
- /// <summary>
- /// Deletes a settings key.
- /// </summary>
- /// <param name="key">The key to delete.</param>
- public void Delete(string key)
- {
- using (RegistryKey pdnKey = CreateSettingsKey(true))
- {
- pdnKey.DeleteValue(key, false);
- }
- }
- /// <summary>
- /// Deletes several settings keys.
- /// </summary>
- /// <param name="keys">The keys to delete.</param>
- public void Delete(string[] keys)
- {
- using (RegistryKey pdnKey = CreateSettingsKey(true))
- {
- foreach (string key in keys)
- {
- pdnKey.DeleteValue(key, false);
- }
- }
- }
- /// <summary>
- /// Retrieves the value of a settings key.
- /// </summary>
- /// <param name="key">The name of the key to retrieve.</param>
- /// <returns>The value of the key.</returns>
- public object GetObject(string key)
- {
- using (RegistryKey pdnKey = CreateSettingsKey(false))
- {
- return pdnKey.GetValue(key);
- }
- }
- /// <summary>
- /// Retrieves the value of a settings key.
- /// </summary>
- /// <param name="key">The name of the key to retrieve.</param>
- /// <param name="defaultValue">The default value to use if the key doesn't exist.</param>
- /// <returns>The value of the key, or defaultValue if it didn't exist.</returns>
- public object GetObject(string key, object defaultValue)
- {
- try
- {
- using (RegistryKey pdnKey = CreateSettingsKey(false))
- {
- return pdnKey.GetValue(key, defaultValue);
- }
- }
- catch (Exception)
- {
- return defaultValue;
- }
- }
- /// <summary>
- /// Sets the value of a settings key.
- /// </summary>
- /// <param name="key">The name of the key to set.</param>
- /// <param name="value">The new value of the key.</param>
- public void SetObject(string key, object value)
- {
- using (RegistryKey pdnKey = CreateSettingsKey(true))
- {
- pdnKey.SetValue(key, value);
- }
- }
- /// <summary>
- /// Retrieves the value of a settings key.
- /// </summary>
- /// <param name="key">The name of the key to retrieve.</param>
- /// <returns>The value of the key.</returns>
- public string GetString(string key)
- {
- return (string)GetObject(key);
- }
- /// <summary>
- /// Retrieves the value of a settings key.
- /// </summary>
- /// <param name="key">The name of the key to retrieve.</param>
- /// <param name="defaultValue">The default value to use if the key doesn't exist.</param>
- /// <returns>The value of the key, or defaultValue if it didn't exist.</returns>
- public string GetString(string key, string defaultValue)
- {
- return (string)GetObject(key, defaultValue);
- }
- /// <summary>
- /// Sets the value of a settings key.
- /// </summary>
- /// <param name="key">The name of the key to set.</param>
- /// <param name="value">The new value of the key.</param>
- public void SetString(string key, string value)
- {
- SetObject(key, value);
- }
- /// <summary>
- /// Saves the given strings.
- /// </summary>
- public void SetStrings(NameValueCollection nvc)
- {
- foreach (string key in nvc.Keys)
- {
- string value = nvc[key];
- SetString("Test\\" + key, value);
- }
- }
- /// <summary>
- /// Retrieves the value of a settings key.
- /// </summary>
- /// <param name="key">The name of the key to retrieve.</param>
- /// <returns>The value of the key.</returns>
- public bool GetBoolean(string key)
- {
- return bool.Parse(GetString(key));
- }
- /// <summary>
- /// Retrieves the value of a settings key.
- /// </summary>
- /// <param name="key">The name of the key to retrieve.</param>
- /// <param name="defaultValue">The default value to use if the key doesn't exist.</param>
- /// <returns>The value of the key, or defaultValue if it didn't exist.</returns>
- public bool GetBoolean(string key, bool defaultValue)
- {
- return bool.Parse(GetString(key, defaultValue.ToString()));
- }
- /// <summary>
- /// Sets the value of a settings key.
- /// </summary>
- /// <param name="key">The name of the key to set.</param>
- /// <param name="value">The new value of the key.</param>
- public void SetBoolean(string key, bool value)
- {
- SetString(key, value.ToString());
- }
- /// <summary>
- /// Retrieves the value of a settings key.
- /// </summary>
- /// <param name="key">The name of the key to retrieve.</param>
- /// <returns>The value of the key.</returns>
- public Point GetPoint(string key)
- {
- int x = GetInt32(key + pointXSuffix);
- int y = GetInt32(key + pointYSuffix);
- return new Point(x, y);
- }
- /// <summary>
- /// Retrieves the value of a settings key.
- /// </summary>
- /// <param name="key">The name of the key to retrieve.</param>
- /// <param name="defaultValue">The default value to use if the key doesn't exist.</param>
- /// <returns>The value of the key, or defaultValue if it didn't exist.</returns>
- 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);
- }
- /// <summary>
- /// Sets the value of a settings key.
- /// </summary>
- /// <param name="key">The name of the key to set.</param>
- /// <param name="value">The new value of the key.</param>
- public void SetPoint(string key, Point value)
- {
- SetInt32(key + pointXSuffix, value.X);
- SetInt32(key + pointYSuffix, value.Y);
- }
- /// <summary>
- /// Retrieves the value of a settings key.
- /// </summary>
- /// <param name="key">The name of the key to retrieve.</param>
- /// <returns>The value of the key.</returns>
- public Int32 GetInt32(string key)
- {
- return Int32.Parse(GetString(key));
- }
- /// <summary>
- /// Retrieves the value of a settings key.
- /// </summary>
- /// <param name="key">The name of the key to retrieve.</param>
- /// <param name="defaultValue">The default value to use if the key doesn't exist.</param>
- /// <returns>The value of the key, or defaultValue if it didn't exist.</returns>
- public Int32 GetInt32(string key, Int32 defaultValue)
- {
- return Int32.Parse(GetString(key, defaultValue.ToString()));
- }
- /// <summary>
- /// Retrieves the value of a settings key.
- /// </summary>
- /// <param name="key">The name of the key to retrieve.</param>
- /// <param name="defaultValue">The default value to use if the key doesn't exist.</param>
- /// <returns>The value of the key, or defaultValue if it didn't exist.</returns>
- public float GetSingle(string key, float defaultValue)
- {
- return Single.Parse(GetString(key, defaultValue.ToString()));
- }
- /// <summary>
- /// Retrieves the value of a settings key.
- /// </summary>
- /// <param name="key">The name of the key to retrieve.</param>
- /// <returns>The value of the key.</returns>
- public float GetSingle(string key)
- {
- return Single.Parse(GetString(key));
- }
- /// <summary>
- /// Sets the value of a settings key.
- /// </summary>
- /// <param name="key">The name of the key to set.</param>
- /// <param name="value">The new value of the key.</param>
- public void SetInt32(string key, int value)
- {
- SetString(key, value.ToString());
- }
- /// <summary>
- /// Sets the value of a settings key.
- /// </summary>
- /// <param name="key">The name of the key to set.</param>
- /// <param name="value">The new value of the key.</param>
- public void SetSingle(string key, float value)
- {
- SetString(key, value.ToString());
- }
- /// <summary>
- /// Gets the value of a settings key.
- /// </summary>
- /// <param name="key">The name of the key to retrieve.</param>
- /// <returns>The value of the key.</returns>
- /// <remarks>This method treats the key value as a stream of base64 encoded bytes that represent a PNG image.</remarks>
- 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;
- }
- /// <summary>
- /// Sets the value of a settings key.
- /// </summary>
- /// <param name="key">The name of the key to set.</param>
- /// <param name="value">The new value of the key.</param>
- /// <remarks>This method saves the key value as a stream of base64 encoded bytes that represent a PNG image.</remarks>
- 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);
- }
- }
- }
|