using System.Configuration;
using System.Linq;
namespace CK.SCP.Utils
{
public static class AppConfigHelper
{
///
/// 获取AppSettings配置节中的Key值
///
/// Key's name
/// Key's value
public static string GetAppValue(string keyName)
{
return ConfigurationManager.AppSettings.Get(keyName);
} /**/
///
/// 获取ConnectionStrings配置节中的值
///
///
public static string GetConnectionString()
{
var settings = ConfigurationManager.ConnectionStrings["ConnString"];
return settings.ConnectionString;
} /**/
///
/// 保存节点中ConnectionStrings的子节点配置项的值
///
///
public static void SetConnectionString(string strConn)
{
var config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.ConnectionStrings.ConnectionStrings["ConnString"].ConnectionString = strConn;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("connectionStrings");
} /**/
///
/// 判断appSettings中是否有此项
///
private static bool IsAppKeyExists(string strKey, Configuration config)
{
return config.AppSettings.Settings.AllKeys.Any(str => str == strKey);
} /**/
///
/// 保存appSettings中某key的value值
///
/// key's name
/// value
public static void SetAppValue(string strKey, string newValue)
{
var config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (!IsAppKeyExists(strKey, config)) return;
config.AppSettings.Settings[strKey].Value = newValue;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
}
}