提交
This commit is contained in:
120
DH.Commons/Helper/SystemConfigHelper.cs
Normal file
120
DH.Commons/Helper/SystemConfigHelper.cs
Normal file
@ -0,0 +1,120 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DH.Commons.Helper
|
||||
{
|
||||
/// <summary>
|
||||
/// 配置文件操作工具类(自动定位主程序配置)
|
||||
/// </summary>
|
||||
public static class SystemConfigHelper
|
||||
{
|
||||
private static Configuration _mainConfig;
|
||||
private static readonly object _lock = new object();
|
||||
|
||||
/// <summary>
|
||||
/// 获取主程序配置对象
|
||||
/// </summary>
|
||||
private static Configuration MainConfiguration
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_mainConfig == null)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (_mainConfig == null)
|
||||
{
|
||||
// 获取主程序路径
|
||||
string exePath = Assembly.GetEntryAssembly().Location;
|
||||
var configFile = exePath + ".config";
|
||||
|
||||
// 加载主程序配置
|
||||
var fileMap = new ExeConfigurationFileMap
|
||||
{
|
||||
ExeConfigFilename = configFile
|
||||
};
|
||||
_mainConfig = ConfigurationManager.OpenMappedExeConfiguration(
|
||||
fileMap,
|
||||
ConfigurationUserLevel.None
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
return _mainConfig;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 检查配置项是否存在
|
||||
/// </summary>
|
||||
public static bool KeyExists(string key)
|
||||
{
|
||||
return MainConfiguration.AppSettings.Settings[key] != null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读取配置项(带类型自动转换)
|
||||
/// </summary>
|
||||
public static T GetValue<T>(string key, T defaultValue = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
var setting = MainConfiguration.AppSettings.Settings[key];
|
||||
if (setting == null) return defaultValue;
|
||||
|
||||
return (T)Convert.ChangeType(setting.Value, typeof(T));
|
||||
}
|
||||
catch
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入配置项(自动保存)
|
||||
/// </summary>
|
||||
public static void SetValue(string key, object value)
|
||||
{
|
||||
var settings = MainConfiguration.AppSettings.Settings;
|
||||
var stringValue = value?.ToString() ?? string.Empty;
|
||||
|
||||
if (settings[key] == null)
|
||||
{
|
||||
settings.Add(key, stringValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
settings[key].Value = stringValue;
|
||||
}
|
||||
|
||||
SaveChanges();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除指定配置项
|
||||
/// </summary>
|
||||
public static void RemoveKey(string key)
|
||||
{
|
||||
if (KeyExists(key))
|
||||
{
|
||||
MainConfiguration.AppSettings.Settings.Remove(key);
|
||||
SaveChanges();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存配置修改
|
||||
/// </summary>
|
||||
private static void SaveChanges()
|
||||
{
|
||||
MainConfiguration.Save(ConfigurationSaveMode.Modified);
|
||||
ConfigurationManager.RefreshSection("appSettings");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user