Settings.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. using Microsoft.Win32;
  2. using System;
  3. using System.Collections.Specialized;
  4. using System.Drawing;
  5. using System.Drawing.Imaging;
  6. using System.IO;
  7. namespace PaintDotNet.SystemLayer
  8. {
  9. /// <summary>
  10. /// Stores non-volatile name/value settings. These persist between sessions of the application.
  11. /// </summary>
  12. /// <remarks>
  13. /// On Windows, this class uses the registry.
  14. /// </remarks>
  15. public sealed class Settings
  16. : ISimpleCollection<string, string>
  17. {
  18. private const string hkcuKey = @"SOFTWARE\Metis Vision";
  19. public static readonly Settings SystemWide = new Settings(Microsoft.Win32.Registry.LocalMachine);
  20. public static readonly Settings CurrentUser = new Settings(Microsoft.Win32.Registry.CurrentUser);
  21. private const string pointXSuffix = ".X";
  22. private const string pointYSuffix = ".Y";
  23. private RegistryKey rootKey;
  24. private Settings(RegistryKey rootKey)
  25. {
  26. this.rootKey = rootKey;
  27. }
  28. private RegistryKey CreateSettingsKey(bool writable)
  29. {
  30. RegistryKey softwareKey = null;
  31. try
  32. {
  33. softwareKey = this.rootKey.OpenSubKey(hkcuKey, writable);
  34. }
  35. catch (Exception)
  36. {
  37. softwareKey = null;
  38. }
  39. if (softwareKey == null)
  40. {
  41. try
  42. {
  43. softwareKey = rootKey.CreateSubKey(hkcuKey);
  44. }
  45. catch (Exception)
  46. {
  47. throw;
  48. }
  49. }
  50. return softwareKey;
  51. }
  52. public bool TryDelete(string key)
  53. {
  54. try
  55. {
  56. Delete(key);
  57. return true;
  58. }
  59. catch (Exception)
  60. {
  61. return false;
  62. }
  63. }
  64. /// <summary>
  65. /// Deletes a settings key.
  66. /// </summary>
  67. /// <param name="key">The key to delete.</param>
  68. public void Delete(string key)
  69. {
  70. using (RegistryKey pdnKey = CreateSettingsKey(true))
  71. {
  72. pdnKey.DeleteValue(key, false);
  73. }
  74. }
  75. /// <summary>
  76. /// Deletes several settings keys.
  77. /// </summary>
  78. /// <param name="keys">The keys to delete.</param>
  79. public void Delete(string[] keys)
  80. {
  81. using (RegistryKey pdnKey = CreateSettingsKey(true))
  82. {
  83. foreach (string key in keys)
  84. {
  85. pdnKey.DeleteValue(key, false);
  86. }
  87. }
  88. }
  89. /// <summary>
  90. /// Retrieves the value of a settings key.
  91. /// </summary>
  92. /// <param name="key">The name of the key to retrieve.</param>
  93. /// <returns>The value of the key.</returns>
  94. public object GetObject(string key)
  95. {
  96. using (RegistryKey pdnKey = CreateSettingsKey(false))
  97. {
  98. return pdnKey.GetValue(key);
  99. }
  100. }
  101. /// <summary>
  102. /// Retrieves the value of a settings key.
  103. /// </summary>
  104. /// <param name="key">The name of the key to retrieve.</param>
  105. /// <param name="defaultValue">The default value to use if the key doesn't exist.</param>
  106. /// <returns>The value of the key, or defaultValue if it didn't exist.</returns>
  107. public object GetObject(string key, object defaultValue)
  108. {
  109. try
  110. {
  111. using (RegistryKey pdnKey = CreateSettingsKey(false))
  112. {
  113. return pdnKey.GetValue(key, defaultValue);
  114. }
  115. }
  116. catch (Exception)
  117. {
  118. return defaultValue;
  119. }
  120. }
  121. /// <summary>
  122. /// Sets the value of a settings key.
  123. /// </summary>
  124. /// <param name="key">The name of the key to set.</param>
  125. /// <param name="value">The new value of the key.</param>
  126. public void SetObject(string key, object value)
  127. {
  128. using (RegistryKey pdnKey = CreateSettingsKey(true))
  129. {
  130. pdnKey.SetValue(key, value);
  131. }
  132. }
  133. /// <summary>
  134. /// Retrieves the value of a settings key.
  135. /// </summary>
  136. /// <param name="key">The name of the key to retrieve.</param>
  137. /// <returns>The value of the key.</returns>
  138. public string GetString(string key)
  139. {
  140. return (string)GetObject(key);
  141. }
  142. /// <summary>
  143. /// Retrieves the value of a settings key.
  144. /// </summary>
  145. /// <param name="key">The name of the key to retrieve.</param>
  146. /// <param name="defaultValue">The default value to use if the key doesn't exist.</param>
  147. /// <returns>The value of the key, or defaultValue if it didn't exist.</returns>
  148. public string GetString(string key, string defaultValue)
  149. {
  150. return (string)GetObject(key, defaultValue);
  151. }
  152. /// <summary>
  153. /// Sets the value of a settings key.
  154. /// </summary>
  155. /// <param name="key">The name of the key to set.</param>
  156. /// <param name="value">The new value of the key.</param>
  157. public void SetString(string key, string value)
  158. {
  159. SetObject(key, value);
  160. }
  161. /// <summary>
  162. /// Saves the given strings.
  163. /// </summary>
  164. public void SetStrings(NameValueCollection nvc)
  165. {
  166. foreach (string key in nvc.Keys)
  167. {
  168. string value = nvc[key];
  169. SetString("Test\\" + key, value);
  170. }
  171. }
  172. /// <summary>
  173. /// Retrieves the value of a settings key.
  174. /// </summary>
  175. /// <param name="key">The name of the key to retrieve.</param>
  176. /// <returns>The value of the key.</returns>
  177. public bool GetBoolean(string key)
  178. {
  179. return bool.Parse(GetString(key));
  180. }
  181. /// <summary>
  182. /// Retrieves the value of a settings key.
  183. /// </summary>
  184. /// <param name="key">The name of the key to retrieve.</param>
  185. /// <param name="defaultValue">The default value to use if the key doesn't exist.</param>
  186. /// <returns>The value of the key, or defaultValue if it didn't exist.</returns>
  187. public bool GetBoolean(string key, bool defaultValue)
  188. {
  189. return bool.Parse(GetString(key, defaultValue.ToString()));
  190. }
  191. /// <summary>
  192. /// Sets the value of a settings key.
  193. /// </summary>
  194. /// <param name="key">The name of the key to set.</param>
  195. /// <param name="value">The new value of the key.</param>
  196. public void SetBoolean(string key, bool value)
  197. {
  198. SetString(key, value.ToString());
  199. }
  200. /// <summary>
  201. /// Retrieves the value of a settings key.
  202. /// </summary>
  203. /// <param name="key">The name of the key to retrieve.</param>
  204. /// <returns>The value of the key.</returns>
  205. public Point GetPoint(string key)
  206. {
  207. int x = GetInt32(key + pointXSuffix);
  208. int y = GetInt32(key + pointYSuffix);
  209. return new Point(x, y);
  210. }
  211. /// <summary>
  212. /// Retrieves the value of a settings key.
  213. /// </summary>
  214. /// <param name="key">The name of the key to retrieve.</param>
  215. /// <param name="defaultValue">The default value to use if the key doesn't exist.</param>
  216. /// <returns>The value of the key, or defaultValue if it didn't exist.</returns>
  217. public Point GetPoint(string key, Point defaultValue)
  218. {
  219. int x = GetInt32(key + pointXSuffix, defaultValue.X);
  220. int y = GetInt32(key + pointYSuffix, defaultValue.Y);
  221. return new Point(x, y);
  222. }
  223. /// <summary>
  224. /// Sets the value of a settings key.
  225. /// </summary>
  226. /// <param name="key">The name of the key to set.</param>
  227. /// <param name="value">The new value of the key.</param>
  228. public void SetPoint(string key, Point value)
  229. {
  230. SetInt32(key + pointXSuffix, value.X);
  231. SetInt32(key + pointYSuffix, value.Y);
  232. }
  233. /// <summary>
  234. /// Retrieves the value of a settings key.
  235. /// </summary>
  236. /// <param name="key">The name of the key to retrieve.</param>
  237. /// <returns>The value of the key.</returns>
  238. public Int32 GetInt32(string key)
  239. {
  240. return Int32.Parse(GetString(key));
  241. }
  242. /// <summary>
  243. /// Retrieves the value of a settings key.
  244. /// </summary>
  245. /// <param name="key">The name of the key to retrieve.</param>
  246. /// <param name="defaultValue">The default value to use if the key doesn't exist.</param>
  247. /// <returns>The value of the key, or defaultValue if it didn't exist.</returns>
  248. public Int32 GetInt32(string key, Int32 defaultValue)
  249. {
  250. return Int32.Parse(GetString(key, defaultValue.ToString()));
  251. }
  252. /// <summary>
  253. /// Retrieves the value of a settings key.
  254. /// </summary>
  255. /// <param name="key">The name of the key to retrieve.</param>
  256. /// <param name="defaultValue">The default value to use if the key doesn't exist.</param>
  257. /// <returns>The value of the key, or defaultValue if it didn't exist.</returns>
  258. public float GetSingle(string key, float defaultValue)
  259. {
  260. return Single.Parse(GetString(key, defaultValue.ToString()));
  261. }
  262. /// <summary>
  263. /// Retrieves the value of a settings key.
  264. /// </summary>
  265. /// <param name="key">The name of the key to retrieve.</param>
  266. /// <returns>The value of the key.</returns>
  267. public float GetSingle(string key)
  268. {
  269. return Single.Parse(GetString(key));
  270. }
  271. /// <summary>
  272. /// Sets the value of a settings key.
  273. /// </summary>
  274. /// <param name="key">The name of the key to set.</param>
  275. /// <param name="value">The new value of the key.</param>
  276. public void SetInt32(string key, int value)
  277. {
  278. SetString(key, value.ToString());
  279. }
  280. /// <summary>
  281. /// Sets the value of a settings key.
  282. /// </summary>
  283. /// <param name="key">The name of the key to set.</param>
  284. /// <param name="value">The new value of the key.</param>
  285. public void SetSingle(string key, float value)
  286. {
  287. SetString(key, value.ToString());
  288. }
  289. /// <summary>
  290. /// Gets the value of a settings key.
  291. /// </summary>
  292. /// <param name="key">The name of the key to retrieve.</param>
  293. /// <returns>The value of the key.</returns>
  294. /// <remarks>This method treats the key value as a stream of base64 encoded bytes that represent a PNG image.</remarks>
  295. public Image GetImage(string key)
  296. {
  297. string imageB64 = GetString(key);
  298. byte[] pngBytes = Convert.FromBase64String(imageB64);
  299. MemoryStream ms = new MemoryStream(pngBytes);
  300. Image image = Image.FromStream(ms);
  301. ms.Close();
  302. return image;
  303. }
  304. /// <summary>
  305. /// Sets the value of a settings key.
  306. /// </summary>
  307. /// <param name="key">The name of the key to set.</param>
  308. /// <param name="value">The new value of the key.</param>
  309. /// <remarks>This method saves the key value as a stream of base64 encoded bytes that represent a PNG image.</remarks>
  310. public void SetImage(string key, Image value)
  311. {
  312. MemoryStream ms = new MemoryStream();
  313. value.Save(ms, ImageFormat.Png);
  314. byte[] buffer = ms.GetBuffer();
  315. string base64 = Convert.ToBase64String(buffer);
  316. SetString(key, base64);
  317. ms.Close();
  318. }
  319. public string Get(string key)
  320. {
  321. return GetString(key);
  322. }
  323. public void Set(string key, string value)
  324. {
  325. SetString(key, value);
  326. }
  327. }
  328. }