Settings.cs 12 KB

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