之前文章:《兩種讀寫配置檔案的方案(app.config與web.config通用)》,現在重新整理一個更完善的版本,增加批量讀寫以及指定配置檔案路徑,程式碼如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Configuration; using System.IO; namespace Zuowj.Utils { /// <summary> /// 配置工具類 /// Author:左文俊 /// Date:2018/1/27 public static class ConfigUtil { /// <summary> /// 獲取管理配置檔案物件Configuration /// </summary> /// <param name="configPath">指定要管理的配置檔案路徑,如果為空或不存在,則管理程式集預設的配置檔案路徑</param> /// <returns></returns> private static Configuration GetConfiguration(string configPath = null) { if (!string.IsNullOrEmpty(configPath) && File.Exists(configPath)) { ExeConfigurationFileMap map = new ExeConfigurationFileMap(); map.ExeConfigFilename = configPath; return ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); } else { return ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); } } /// <summary> /// 獲取指定配置檔案+配置名稱的配置項的值 /// </summary> public static string GetAppSettingValue(string key, string defaultValue = null, string configPath = null) { var config = GetConfiguration(configPath); var appSetting = config.AppSettings.Settings[key]; return appSetting.Value; } /// <summary> /// 設定配置值(存在則更新,不存在則新增) /// </summary> public static void SetAppSettingValue(string key, string value, string configPath = null) { var config = GetConfiguration(configPath); var setting = config.AppSettings.Settings[key]; if (setting == null) { config.AppSettings.Settings.Add(key, value); } else { setting.Value = value; } config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); } /// <summary> /// 刪除配置值 /// </summary> public static void RemoveAppSetting(string key, string configPath = null) { var config = GetConfiguration(configPath); config.AppSettings.Settings.Remove(key); config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); } /// <summary> /// 設定多個配置值(存在則更新,不存在則新增) /// </summary> public static void SetAppSettingValues(IEnumerable<KeyValuePair<string, string>> settingValues, string configPath = null) { var config = GetConfiguration(configPath); foreach (var item in settingValues) { var setting = config.AppSettings.Settings[item.Key]; if (setting == null) { config.AppSettings.Settings.Add(item.Key, item.Value); } else { setting.Value = item.Value; } } config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); } /// <summary> /// 獲取所有配置值 /// </summary> public static Dictionary<string, string> GetAppSettingValues(string configPath = null) { Dictionary<string, string> settingDic = new Dictionary<string, string>(); var config = GetConfiguration(configPath); var settings = config.AppSettings.Settings; foreach (string key in settings.AllKeys) { settingDic[key] = settings[key].ToString(); } return settingDic; } /// <summary> /// 刪除多個配置值 /// </summary> public static void RemoveAppSettings(string configPath = null, params string[] keys) { var config = GetConfiguration(configPath); if (keys != null) { foreach (string key in keys) { config.AppSettings.Settings.Remove(key); } } else { config.AppSettings.Settings.Clear(); } config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); } /// <summary> /// 獲取連線字串 /// </summary> public static string GetConnectionString(string name, string defaultconnStr = null, string configPath = null) { var config = GetConfiguration(configPath); var connStrSettings = config.ConnectionStrings.ConnectionStrings[name]; if (connStrSettings == null) { return defaultconnStr; } return connStrSettings.ConnectionString; } /// <summary> /// 獲取指定配置檔案+連線名稱的連線字串配置項 /// </summary> public static ConnectionStringSettings GetConnectionStringSetting(string name, string configPath = null) { var config = GetConfiguration(configPath); var connStrSettings = config.ConnectionStrings.ConnectionStrings[name]; return connStrSettings; } /// <summary> /// 設定連線字串的值(存在則更新,不存在則新增) /// </summary> public static void SetConnectionString(string name, string connstr, string provider, string configPath = null) { var config = GetConfiguration(configPath); ConnectionStringSettings connStrSettings = config.ConnectionStrings.ConnectionStrings[name]; if (connStrSettings != null) { connStrSettings.ConnectionString = connstr; connStrSettings.ProviderName = provider; } else { connStrSettings = new ConnectionStringSettings(name, connstr, provider); config.ConnectionStrings.ConnectionStrings.Add(connStrSettings); } config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("connectionStrings"); } /// <summary> /// 刪除連線字串配置項 /// </summary> public static void RemoveConnectionString(string name, string configPath = null) { var config = GetConfiguration(configPath); config.ConnectionStrings.ConnectionStrings.Remove(name); config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("connectionStrings"); } /// <summary> /// 獲取所有的連線字串配置項 /// </summary> public static Dictionary<string, ConnectionStringSettings> GetConnectionStringSettings(string configPath = null) { var config = GetConfiguration(configPath); var connStrSettingDic = new Dictionary<string, ConnectionStringSettings>(); var connStrSettings = ConfigurationManager.ConnectionStrings; foreach (ConnectionStringSettings item in connStrSettings) { connStrSettingDic[item.Name] = item; } return connStrSettingDic; } /// <summary> /// 設定多個連線字串的值(存在則更新,不存在則新增) /// </summary> public static void SetConnectionStrings(IEnumerable<ConnectionStringSettings> connStrSettings, string configPath = null) { var config = GetConfiguration(configPath); foreach (var item in connStrSettings) { ConnectionStringSettings connStrSetting = config.ConnectionStrings.ConnectionStrings[item.Name]; if (connStrSetting != null) { connStrSetting.ConnectionString = item.ConnectionString; connStrSetting.ProviderName = item.ProviderName; } else { config.ConnectionStrings.ConnectionStrings.Add(item); } } config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("connectionStrings"); } /// <summary> /// 刪除多個連線字串配置項 /// </summary> public static void RemoveConnectionStrings(string configPath = null, params string[] names) { var config = GetConfiguration(configPath); if (names != null) { foreach (string name in names) { config.ConnectionStrings.ConnectionStrings.Remove(name); } } else { config.ConnectionStrings.ConnectionStrings.Clear(); } config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("connectionStrings"); } } }