RegistryConfiguration.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Reflection;
  4. using InABox.Configuration;
  5. using Microsoft.Win32;
  6. namespace PRS.Shared
  7. {
  8. /*public abstract class RegistryConfigurationSettings : IConfigurationSettings
  9. {
  10. public abstract void Load(RegistryKey key);
  11. public abstract void Save(RegistryKey key);
  12. }
  13. public class RegistryConfiguration<T> : Configuration<T> where T : RegistryConfigurationSettings, new()
  14. {
  15. private readonly RegistryKey _key;
  16. public RegistryConfiguration(string section = "") : base(section)
  17. {
  18. _key = Registry.LocalMachine.OpenSubKey("Software");
  19. _key = OpenOrCreate(_key, "PRS");
  20. _key = OpenOrCreate(_key, Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location));
  21. if (string.IsNullOrWhiteSpace(section))
  22. _key = OpenOrCreate(_key, section);
  23. }
  24. public override T Load()
  25. {
  26. var cached = ConfigurationCache.Check<T>(ConfigurationCacheType.Local, Section);
  27. if (cached != null)
  28. //Logger.Send(LogType.Information, "", "Local Config: Returning Cached Configuration Data");
  29. return cached;
  30. //Logger.Send(LogType.Information, "", "Local Config: Creating "+typeof(T).EntityName().Split('.').Last());
  31. var result = new T();
  32. result.Load(_key);
  33. //Logger.Send(LogType.Information, "", "Local Config: Adding Configuration to Cache");
  34. ConfigurationCache.Add(ConfigurationCacheType.Local, Section, result);
  35. return result;
  36. }
  37. public override Dictionary<string, T> LoadAll()
  38. {
  39. var results = new Dictionary<string, T>();
  40. var sections = _key.GetSubKeyNames();
  41. foreach (var section in sections)
  42. {
  43. var _subkey = _key.OpenSubKey(section, true);
  44. var result = new T();
  45. result.Load(_subkey);
  46. results[section] = result;
  47. }
  48. return results;
  49. }
  50. public override void Save(T obj)
  51. {
  52. obj.Save(_key);
  53. ConfigurationCache.Add(ConfigurationCacheType.Local, Section, obj);
  54. }
  55. public override void SaveAll(Dictionary<string, T> objs)
  56. {
  57. throw new System.NotImplementedException();
  58. }
  59. public override void Delete()
  60. {
  61. var key = GetParent();
  62. if (string.IsNullOrWhiteSpace(Section))
  63. key.DeleteSubKeyTree(Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location));
  64. else
  65. key.DeleteSubKeyTree(Section);
  66. ConfigurationCache.Clear<T>(ConfigurationCacheType.Local, Section);
  67. }
  68. public override string[] Sections()
  69. {
  70. return _key.GetSubKeyNames();
  71. }
  72. public RegistryKey GetParent()
  73. {
  74. var result = Registry.LocalMachine.OpenSubKey("Software");
  75. result = OpenOrCreate(result, "PRS");
  76. if (!string.IsNullOrWhiteSpace(Section))
  77. result = OpenOrCreate(result, Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().Location));
  78. return result;
  79. }
  80. private RegistryKey OpenOrCreate(RegistryKey key, string name)
  81. {
  82. var result = key.OpenSubKey(name, true);
  83. if (result == null)
  84. result = key.CreateSubKey(name, true);
  85. return result;
  86. }
  87. }*/
  88. }