157 lines
4.1 KiB
C#
157 lines
4.1 KiB
C#
using MAX.Framework.Commons;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
|
|
namespace MAX.Framework.Helpers
|
|
{
|
|
/// <summary>
|
|
/// App 配置
|
|
/// </summary>
|
|
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;
|
|
}
|
|
///<summary>
|
|
///返回*.exe.config文件中appSettings配置节的value项
|
|
///</summary>
|
|
///<param name="strKey"></param>
|
|
///<returns></returns>
|
|
public static TValue GetAppConfig<TValue>(string strKey, TValue def = default)
|
|
{
|
|
try
|
|
{
|
|
string value = GetAppConfig(strKey);
|
|
return MAX.Core.Helpers.Convert.To<TValue>(value);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return def;
|
|
}
|
|
}
|
|
///<summary>
|
|
///返回*.exe.config文件中appSettings配置节的value项
|
|
///</summary>
|
|
///<param name="strKey"></param>
|
|
///<returns></returns>
|
|
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<KeyValue> GetAppSettings()
|
|
{
|
|
Configuration config = ConfigurationManager.OpenExeConfiguration(exeConfigPath);
|
|
List<KeyValue> settings = new List<KeyValue>();
|
|
foreach (string key in config.AppSettings.Settings.AllKeys)
|
|
{
|
|
settings.Add(new KeyValue(key, config.AppSettings.Settings[key].Value));
|
|
|
|
}
|
|
return settings;
|
|
}
|
|
|
|
///<summary>
|
|
///在*.exe.config文件中appSettings配置节增加一对键值对
|
|
///</summary>
|
|
///<param name="newKey"></param>
|
|
///<param name="newValue"></param>
|
|
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");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 静态类型写入配置文件信息
|
|
/// 名称相同
|
|
/// </summary>
|
|
/// <param name="appType"></param>
|
|
public static void SetAppConfig(Type appType)
|
|
{
|
|
PropertyInfo[] props = appType.GetProperties(BindingFlags.Static | BindingFlags.Public);
|
|
List<KeyValue> 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
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 静态配置文件信息写入到配置文件中
|
|
/// 只写入配置文件中存在的配置项中
|
|
/// </summary>
|
|
/// <param name="appType"></param>
|
|
public static void UpdateAppConfig(Type appType)
|
|
{
|
|
PropertyInfo[] props = appType.GetProperties(BindingFlags.Static | BindingFlags.Public);
|
|
List<KeyValue> 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
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|