using System.Configuration; using System.Collections.Specialized; namespace qdr.app.bundleboiler.Configuration { public class AppSettingsHandler { private readonly string _configPath; private readonly System.Configuration.Configuration _configuration; #region *** Constructors *** /// /// Initializes a new instance of AppSettingsHandler using the default application configuration /// public AppSettingsHandler() : this(GetDefaultConfigPath()) { } /// /// Initializes a new instance of AppSettingsHandler with a specific configuration file path /// /// Full path to the configuration file public AppSettingsHandler(string configPath) { if (string.IsNullOrEmpty(configPath)) throw new ArgumentNullException(nameof(configPath)); if (!File.Exists(configPath)) throw new FileNotFoundException("Configuration file not found", configPath); _configPath = configPath; _configuration = ConfigurationManager.OpenExeConfiguration(configPath); } #endregion #region *** Public Read Methods *** /// /// Gets a string value from AppSettings /// /// The key to look up /// Optional default value if key is not found /// The value from settings or default value public string GetString(string key, string defaultValue = "") { return ConfigurationManager.AppSettings[key] ?? defaultValue; } /// /// Gets an integer value from AppSettings /// /// The key to look up /// Optional default value if key is not found or parsing fails /// The value from settings or default value public int GetInt(string key, int defaultValue = 0) { string? value = ConfigurationManager.AppSettings[key]; return int.TryParse(value, out int result) ? result : defaultValue; } /// /// Gets a boolean value from AppSettings /// /// The key to look up /// Optional default value if key is not found or parsing fails /// The value from settings or default value public bool GetBool(string key, bool defaultValue = false) { string? value = ConfigurationManager.AppSettings[key]; return bool.TryParse(value, out bool result) ? result : defaultValue; } /// /// Gets a DateTime value from AppSettings /// /// The key to look up /// Optional default value if key is not found or parsing fails /// The value from settings or default value public DateTime GetDateTime(string key, DateTime? defaultValue = null) { string? value = ConfigurationManager.AppSettings[key]; return DateTime.TryParse(value, out DateTime result) ? result : defaultValue ?? DateTime.MinValue; } /// /// Gets an enum value from AppSettings /// /// The enum type /// The key to look up /// Optional default value if key is not found or parsing fails /// The value from settings or default value public T GetEnum(string key, T defaultValue) where T : struct { string? value = ConfigurationManager.AppSettings[key]; return Enum.TryParse(value, true, out T result) ? result : defaultValue; } /// /// Checks if a key exists in AppSettings /// /// The key to check /// True if the key exists public bool HasKey(string key) { return ConfigurationManager.AppSettings[key] != null; } /// /// Gets all settings as a NameValueCollection /// /// NameValueCollection containing all settings public NameValueCollection GetAllSettings() { return ConfigurationManager.AppSettings; } #endregion #region *** Public Write Methods *** /// /// Sets a string value in AppSettings /// /// The key to set /// The value to store public void SetString(string key, string value) { SetValue(key, value); } /// /// Sets an integer value in AppSettings /// /// The key to set /// The value to store public void SetInt(string key, int value) { SetValue(key, value.ToString()); } /// /// Sets a boolean value in AppSettings /// /// The key to set /// The value to store public void SetBool(string key, bool value) { SetValue(key, value.ToString()); } /// /// Sets a DateTime value in AppSettings /// /// The key to set /// The value to store public void SetDateTime(string key, DateTime value) { SetValue(key, value.ToString("o")); // ISO 8601 format } /// /// Sets an enum value in AppSettings /// /// The enum type /// The key to set /// The value to store public void SetEnum(string key, T value) where T : struct { SetValue(key, value.ToString()); } /// /// Removes a setting from AppSettings /// /// The key to remove public void RemoveSetting(string key) { if (HasKey(key)) { _configuration.AppSettings.Settings.Remove(key); SaveConfiguration(); } } /// /// Clear all settings from AppSettings /// public void ClearSettings() { _configuration.AppSettings.Settings.Clear(); SaveConfiguration(); } #endregion #region Private Methods private void SetValue(string key, string? value) { try { if (HasKey(key)) { _configuration.AppSettings.Settings[key].Value = value; } else { _configuration.AppSettings.Settings.Add(key, value); } SaveConfiguration(); } catch (Exception ex) { throw new ConfigurationErrorsException($"Error setting configuration value for key: {key}", ex); } } private static string GetDefaultConfigPath() { string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location; return exePath + ".config"; } private void SaveConfiguration() { try { _configuration.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); } catch (Exception ex) { throw new ConfigurationErrorsException("Error saving configuration changes", ex); } } #endregion } }