AppSettingsHandler.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using System.Configuration;
  2. using System.Collections.Specialized;
  3. namespace qdr.app.bundleboiler.Configuration
  4. {
  5. public class AppSettingsHandler
  6. {
  7. private readonly string _configPath;
  8. private readonly System.Configuration.Configuration _configuration;
  9. #region *** Constructors ***
  10. /// <summary>
  11. /// Initializes a new instance of AppSettingsHandler using the default application configuration
  12. /// </summary>
  13. public AppSettingsHandler()
  14. : this(GetDefaultConfigPath())
  15. {
  16. }
  17. /// <summary>
  18. /// Initializes a new instance of AppSettingsHandler with a specific configuration file path
  19. /// </summary>
  20. /// <param name="configPath">Full path to the configuration file</param>
  21. public AppSettingsHandler(string configPath)
  22. {
  23. if (string.IsNullOrEmpty(configPath))
  24. throw new ArgumentNullException(nameof(configPath));
  25. if (!File.Exists(configPath))
  26. throw new FileNotFoundException("Configuration file not found", configPath);
  27. _configPath = configPath;
  28. _configuration = ConfigurationManager.OpenExeConfiguration(configPath);
  29. }
  30. #endregion
  31. #region *** Public Read Methods ***
  32. /// <summary>
  33. /// Gets a string value from AppSettings
  34. /// </summary>
  35. /// <param name="key">The key to look up</param>
  36. /// <param name="defaultValue">Optional default value if key is not found</param>
  37. /// <returns>The value from settings or default value</returns>
  38. public string GetString(string key, string defaultValue = "")
  39. {
  40. return ConfigurationManager.AppSettings[key] ?? defaultValue;
  41. }
  42. /// <summary>
  43. /// Gets an integer value from AppSettings
  44. /// </summary>
  45. /// <param name="key">The key to look up</param>
  46. /// <param name="defaultValue">Optional default value if key is not found or parsing fails</param>
  47. /// <returns>The value from settings or default value</returns>
  48. public int GetInt(string key, int defaultValue = 0)
  49. {
  50. string? value = ConfigurationManager.AppSettings[key];
  51. return int.TryParse(value, out int result) ? result : defaultValue;
  52. }
  53. /// <summary>
  54. /// Gets a boolean value from AppSettings
  55. /// </summary>
  56. /// <param name="key">The key to look up</param>
  57. /// <param name="defaultValue">Optional default value if key is not found or parsing fails</param>
  58. /// <returns>The value from settings or default value</returns>
  59. public bool GetBool(string key, bool defaultValue = false)
  60. {
  61. string? value = ConfigurationManager.AppSettings[key];
  62. return bool.TryParse(value, out bool result) ? result : defaultValue;
  63. }
  64. /// <summary>
  65. /// Gets a DateTime value from AppSettings
  66. /// </summary>
  67. /// <param name="key">The key to look up</param>
  68. /// <param name="defaultValue">Optional default value if key is not found or parsing fails</param>
  69. /// <returns>The value from settings or default value</returns>
  70. public DateTime GetDateTime(string key, DateTime? defaultValue = null)
  71. {
  72. string? value = ConfigurationManager.AppSettings[key];
  73. return DateTime.TryParse(value, out DateTime result) ? result : defaultValue ?? DateTime.MinValue;
  74. }
  75. /// <summary>
  76. /// Gets an enum value from AppSettings
  77. /// </summary>
  78. /// <typeparam name="T">The enum type</typeparam>
  79. /// <param name="key">The key to look up</param>
  80. /// <param name="defaultValue">Optional default value if key is not found or parsing fails</param>
  81. /// <returns>The value from settings or default value</returns>
  82. public T GetEnum<T>(string key, T defaultValue) where T : struct
  83. {
  84. string? value = ConfigurationManager.AppSettings[key];
  85. return Enum.TryParse<T>(value, true, out T result) ? result : defaultValue;
  86. }
  87. /// <summary>
  88. /// Checks if a key exists in AppSettings
  89. /// </summary>
  90. /// <param name="key">The key to check</param>
  91. /// <returns>True if the key exists</returns>
  92. public bool HasKey(string key)
  93. {
  94. return ConfigurationManager.AppSettings[key] != null;
  95. }
  96. /// <summary>
  97. /// Gets all settings as a NameValueCollection
  98. /// </summary>
  99. /// <returns>NameValueCollection containing all settings</returns>
  100. public NameValueCollection GetAllSettings()
  101. {
  102. return ConfigurationManager.AppSettings;
  103. }
  104. #endregion
  105. #region *** Public Write Methods ***
  106. /// <summary>
  107. /// Sets a string value in AppSettings
  108. /// </summary>
  109. /// <param name="key">The key to set</param>
  110. /// <param name="value">The value to store</param>
  111. public void SetString(string key, string value)
  112. {
  113. SetValue(key, value);
  114. }
  115. /// <summary>
  116. /// Sets an integer value in AppSettings
  117. /// </summary>
  118. /// <param name="key">The key to set</param>
  119. /// <param name="value">The value to store</param>
  120. public void SetInt(string key, int value)
  121. {
  122. SetValue(key, value.ToString());
  123. }
  124. /// <summary>
  125. /// Sets a boolean value in AppSettings
  126. /// </summary>
  127. /// <param name="key">The key to set</param>
  128. /// <param name="value">The value to store</param>
  129. public void SetBool(string key, bool value)
  130. {
  131. SetValue(key, value.ToString());
  132. }
  133. /// <summary>
  134. /// Sets a DateTime value in AppSettings
  135. /// </summary>
  136. /// <param name="key">The key to set</param>
  137. /// <param name="value">The value to store</param>
  138. public void SetDateTime(string key, DateTime value)
  139. {
  140. SetValue(key, value.ToString("o")); // ISO 8601 format
  141. }
  142. /// <summary>
  143. /// Sets an enum value in AppSettings
  144. /// </summary>
  145. /// <typeparam name="T">The enum type</typeparam>
  146. /// <param name="key">The key to set</param>
  147. /// <param name="value">The value to store</param>
  148. public void SetEnum<T>(string key, T value) where T : struct
  149. {
  150. SetValue(key, value.ToString());
  151. }
  152. /// <summary>
  153. /// Removes a setting from AppSettings
  154. /// </summary>
  155. /// <param name="key">The key to remove</param>
  156. public void RemoveSetting(string key)
  157. {
  158. if (HasKey(key))
  159. {
  160. _configuration.AppSettings.Settings.Remove(key);
  161. SaveConfiguration();
  162. }
  163. }
  164. /// <summary>
  165. /// Clear all settings from AppSettings
  166. /// </summary>
  167. public void ClearSettings()
  168. {
  169. _configuration.AppSettings.Settings.Clear();
  170. SaveConfiguration();
  171. }
  172. #endregion
  173. #region Private Methods
  174. private void SetValue(string key, string? value)
  175. {
  176. try
  177. {
  178. if (HasKey(key))
  179. {
  180. _configuration.AppSettings.Settings[key].Value = value;
  181. }
  182. else
  183. {
  184. _configuration.AppSettings.Settings.Add(key, value);
  185. }
  186. SaveConfiguration();
  187. }
  188. catch (Exception ex)
  189. {
  190. throw new ConfigurationErrorsException($"Error setting configuration value for key: {key}", ex);
  191. }
  192. }
  193. private static string GetDefaultConfigPath()
  194. {
  195. string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
  196. return exePath + ".config";
  197. }
  198. private void SaveConfiguration()
  199. {
  200. try
  201. {
  202. _configuration.Save(ConfigurationSaveMode.Modified);
  203. ConfigurationManager.RefreshSection("appSettings");
  204. }
  205. catch (Exception ex)
  206. {
  207. throw new ConfigurationErrorsException("Error saving configuration changes", ex);
  208. }
  209. }
  210. #endregion
  211. }
  212. }