using System.Configuration;
using System.IO;
/// <summary>
/// 配置文件辅助类
/// </summary>
public class ConfigHelper
{
/// <summary>
/// 获得指定配置节点的值
/// 节点不存在时返回 null
/// </summary>
public static string ReadAppConfig(string strKey)
{
if (ConfigurationManager.AppSettings[strKey] == null)
return null;
return ConfigurationManager.AppSettings[strKey].ToString();
}
/// <summary>
/// 读 ConnectionStrings 节点 ConnectionName 的连接字符串
/// 节点不存在时返回 null
/// </summary>
public static string ReadConnectionStringConfig(string ConnectionName)
{
if (ConfigurationManager.ConnectionStrings[ConnectionName] == null)
return null;
return ConfigurationManager.ConnectionStrings[ConnectionName].ConnectionString.ToString();
}
/// <summary>
/// 修改指定配置节点的值
/// 节点不存在则添加
/// </summary>
public static void WriteAppConfig(string newKey, string newValue)
{
// 打开配置文件
//Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (ConfigurationManager.AppSettings[newKey] != null)
config.AppSettings.Settings.Remove(newKey);
// 添加新的键-值对
config.AppSettings.Settings.Add(newKey, newValue);
// 保存对配置文件的更改
config.Save(ConfigurationSaveMode.Minimal);
ConfigurationManager.RefreshSection("appSettings");
}
/// <summary>
/// 写 ConnectionStrings 节点 ConnectionName 的连接字符串
/// 节点不存在则添加
/// </summary>
public static void WriteConnectionStringConfig(string newName, string newConString, string newProvideName)
{
// 打开配置文件
//Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (ConfigurationManager.ConnectionStrings[newName] != null)
config.ConnectionStrings.ConnectionStrings.Remove(newName);
// 添加新的连接字符串
config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(newName, newConString, newProvideName));
// 保存对配置文件的更改
config.Save(ConfigurationSaveMode.Minimal);
ConfigurationManager.RefreshSection("connectionStrings");
}
/// <summary>
/// 重新读取配置文件
/// </summary>
public static void RefreshConfigs()
{
ConfigurationManager.RefreshSection("connectionStrings");
ConfigurationManager.RefreshSection("appSettings");
}
// 2016年12月28日17时28分42秒 6 扩展,读其它配置文件
public static string ReadAppConfig(string filePath, string strKey)
{
if (File.Exists(filePath))
{
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = filePath;
Configuration cfg = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
if (cfg.AppSettings.Settings[strKey] == null)
return null;
return cfg.AppSettings.Settings[strKey].Value;
}
throw new FileNotFoundException(filePath + " 指定文件不存在!");
}
public static string ReadConnectionStringConfig(string filePath, string ConnectionName)
{
if (File.Exists(filePath))
{
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = filePath;
Configuration cfg = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
if (cfg.ConnectionStrings.ConnectionStrings[ConnectionName] == null)
return null;
return cfg.ConnectionStrings.ConnectionStrings[ConnectionName].ConnectionString;
}
throw new FileNotFoundException(filePath + " 指定文件不存在!");
}
public static void WriteAppConfig(string filePath, string newKey, string newValue)
{
if (File.Exists(filePath))
{
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = filePath;
Configuration cfg = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
if (cfg.AppSettings.Settings[newKey] != null)
cfg.AppSettings.Settings.Remove(newKey);
cfg.AppSettings.Settings.Add(newKey, newValue);
// 保存对配置文件的更改
cfg.Save(ConfigurationSaveMode.Minimal);
return;
}
throw new FileNotFoundException(filePath + " 指定文件不存在!");
}
public static void WriteConnectionStringConfig(string filePath, string newName, string newConString, string newProvideName)
{
if(File.Exists(filePath))
{
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = filePath;
Configuration cfg = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
if (cfg.ConnectionStrings.ConnectionStrings[newName] != null)
cfg.ConnectionStrings.ConnectionStrings.Remove(newName);
cfg.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(newName, newConString, newProvideName));
// 保存对配置文件的更改
cfg.Save(ConfigurationSaveMode.Minimal);
return;
}
throw new FileNotFoundException(filePath + " 指定文件不存在!");
}
}
部分内容摘自:http://www.jb51.net/article/34476.htm