using MAX.Framework.Commons; using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Reflection; namespace MAX.Framework.Helpers { /// /// App 配置 /// public class AppConfigHelper { public static string exeConfigPath = Assembly.GetEntryAssembly()?.Location ?? ""; public static string GetConnectionString(string name) { Configuration config = ConfigurationManager.OpenExeConfiguration(exeConfigPath); string connection = config.ConnectionStrings.ConnectionStrings[name].ConnectionString ?? ""; //string connectionString = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString; return connection; } /// ///返回*.exe.config文件中appSettings配置节的value项 /// /// /// public static TValue GetAppConfig(string strKey, TValue def = default) { try { string value = GetAppConfig(strKey); return MAX.Core.Helpers.Convert.To(value); } catch (Exception ex) { return def; } } /// ///返回*.exe.config文件中appSettings配置节的value项 /// /// /// public static string GetAppConfig(string strKey) { Configuration config = ConfigurationManager.OpenExeConfiguration(exeConfigPath); foreach (string key in config.AppSettings.Settings.AllKeys) { if (key == strKey) { return config.AppSettings.Settings[strKey].Value.ToString(); } } return null; } public static List GetAppSettings() { Configuration config = ConfigurationManager.OpenExeConfiguration(exeConfigPath); List settings = new List(); foreach (string key in config.AppSettings.Settings.AllKeys) { settings.Add(new KeyValue(key, config.AppSettings.Settings[key].Value)); } return settings; } /// ///在*.exe.config文件中appSettings配置节增加一对键值对 /// /// /// public static void UpdateAppConfig(string newKey, string newValue) { Configuration config = ConfigurationManager.OpenExeConfiguration(exeConfigPath); bool exist = false; foreach (string key in config.AppSettings.Settings.AllKeys) { if (key == newKey) { exist = true; } } if (exist) { config.AppSettings.Settings.Remove(newKey); } config.AppSettings.Settings.Add(newKey, newValue); config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); } /// /// 静态类型写入配置文件信息 /// 名称相同 /// /// public static void SetAppConfig(Type appType) { PropertyInfo[] props = appType.GetProperties(BindingFlags.Static | BindingFlags.Public); List settings = GetAppSettings(); foreach (KeyValue item in settings) { try { PropertyInfo prop = props.FirstOrDefault(a => a.Name.ToLower() == item.Key.ToLower()); if (prop != null) { object value = Core.Helpers.Convert.To(prop.PropertyType, item.Value); prop.SetValue(null, value); } } finally { } } } /// /// 静态配置文件信息写入到配置文件中 /// 只写入配置文件中存在的配置项中 /// /// public static void UpdateAppConfig(Type appType) { PropertyInfo[] props = appType.GetProperties(BindingFlags.Static | BindingFlags.Public); List settings = GetAppSettings(); foreach (KeyValue item in settings) { try { PropertyInfo prop = props.FirstOrDefault(a => a.Name.ToLower() == item.Key.ToLower()); if (prop != null) { object value = prop.GetValue(null); if (value?.ToString().ToLower() == item.Value.ToLower()) { continue; } UpdateAppConfig(item.Key, value?.ToString()); } } finally { } } } } }