操作App.config与Web.config文件
对于动态操作配置文件我想大家都不陌生,以前都是用的操作xml的方式,在.net2.0以上版本我可以利用新的方法来实现
自己对自己的配置:
public class ConfigureAppConfig
{
//静态构造,不能实例化
static ConfigureAppConfig() { }
/// <summary>
/// 获取AppSettings配置节中的Key值
/// </summary>
/// <param name="keyName">Key's name</param>
/// <returns>Key's value</returns>
public static string GetAppSettingsKeyValue(string keyName)
{
return ConfigurationManager.AppSettings.Get(keyName);
}
/// <summary>
/// 获取ConnectionStrings配置节中的值
/// </summary>
/// <returns></returns>
public static string GetConnectionStringsElementValue()
{
ConnectionStringSettings settings = System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"];
return settings.ConnectionString;
}
/// <summary>
/// 保存节点中ConnectionStrings的子节点配置项的值
/// </summary>
/// <param name="elementValue"></param>
public static void ConnectionStringsSave(string ConnectionStringsName, string elementValue)
{
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.ConnectionStrings.ConnectionStrings["connectionString"].ConnectionString = elementValue;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("connectionStrings");
}
/// <summary>
/// 判断appSettings中是否有此项
/// </summary>
private static bool AppSettingsKeyExists(string strKey, Configuration config)
{
foreach (string str in config.AppSettings.Settings.AllKeys)
{
if (str == strKey)
{
return true;
}
}
return false;
}
/// <summary>
/// 保存appSettings中某key的value值
/// </summary>
/// <param name="strKey">key's name</param>
/// <param name="newValue">value</param>
public static void AppSettingsSave(string strKey, string newValue)
{
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (AppSettingsKeyExists(strKey, config))
{
config.AppSettings.Settings[strKey].Value = newValue;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
}
}
{
//静态构造,不能实例化
static ConfigureAppConfig() { }
/// <summary>
/// 获取AppSettings配置节中的Key值
/// </summary>
/// <param name="keyName">Key's name</param>
/// <returns>Key's value</returns>
public static string GetAppSettingsKeyValue(string keyName)
{
return ConfigurationManager.AppSettings.Get(keyName);
}
/// <summary>
/// 获取ConnectionStrings配置节中的值
/// </summary>
/// <returns></returns>
public static string GetConnectionStringsElementValue()
{
ConnectionStringSettings settings = System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"];
return settings.ConnectionString;
}
/// <summary>
/// 保存节点中ConnectionStrings的子节点配置项的值
/// </summary>
/// <param name="elementValue"></param>
public static void ConnectionStringsSave(string ConnectionStringsName, string elementValue)
{
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.ConnectionStrings.ConnectionStrings["connectionString"].ConnectionString = elementValue;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("connectionStrings");
}
/// <summary>
/// 判断appSettings中是否有此项
/// </summary>
private static bool AppSettingsKeyExists(string strKey, Configuration config)
{
foreach (string str in config.AppSettings.Settings.AllKeys)
{
if (str == strKey)
{
return true;
}
}
return false;
}
/// <summary>
/// 保存appSettings中某key的value值
/// </summary>
/// <param name="strKey">key's name</param>
/// <param name="newValue">value</param>
public static void AppSettingsSave(string strKey, string newValue)
{
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (AppSettingsKeyExists(strKey, config))
{
config.AppSettings.Settings[strKey].Value = newValue;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
}
}
如果你的程序是对其它程序的配置文件进行操作,代码如下:
ExeConfigurationFileMap filemap = new ExeConfigurationFileMap();
filemap.ExeConfigFilename = filePath;//配置文件路径
config = ConfigurationManager.OpenMappedExeConfiguration(filemap, ConfigurationUserLevel.None);
if (AppSettingsKeyExists("Refresh", config))
{
config.AppSettings.Settings["Refresh"].Value = M_TimeRead.ToString();
}
if (AppSettingsKeyExists("MachineNo", config))
{
config.AppSettings.Settings["MachineNo"].Value = M_MachineNo;
}
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
config.ConnectionStrings.ConnectionStrings["connectionString"].ConnectionString = M_ConnectionString;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("connectionStrings");
filemap.ExeConfigFilename = filePath;//配置文件路径
config = ConfigurationManager.OpenMappedExeConfiguration(filemap, ConfigurationUserLevel.None);
if (AppSettingsKeyExists("Refresh", config))
{
config.AppSettings.Settings["Refresh"].Value = M_TimeRead.ToString();
}
if (AppSettingsKeyExists("MachineNo", config))
{
config.AppSettings.Settings["MachineNo"].Value = M_MachineNo;
}
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
config.ConnectionStrings.ConnectionStrings["connectionString"].ConnectionString = M_ConnectionString;
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("connectionStrings");
数据库字符串加密
ExeConfigurationFileMap filemap = new ExeConfigurationFileMap();
filemap.ExeConfigFilename = Application.ExecutablePath + ".Config"; //filePath;
config = ConfigurationManager.OpenMappedExeConfiguration(filemap, ConfigurationUserLevel.None);
//指定我所要的节点
ConfigurationSection section = config.ConnectionStrings;
if ((section.SectionInformation.IsProtected == false) && (section.ElementInformation.IsLocked == false))
{
//制定节点加密
section.SectionInformation.ProtectSection(protect);
//即使没有修改也保存设置
section.SectionInformation.ForceSave = true;
//配置文件内容保存到xml
config.Save(ConfigurationSaveMode.Full);
}
filemap.ExeConfigFilename = Application.ExecutablePath + ".Config"; //filePath;
config = ConfigurationManager.OpenMappedExeConfiguration(filemap, ConfigurationUserLevel.None);
//指定我所要的节点
ConfigurationSection section = config.ConnectionStrings;
if ((section.SectionInformation.IsProtected == false) && (section.ElementInformation.IsLocked == false))
{
//制定节点加密
section.SectionInformation.ProtectSection(protect);
//即使没有修改也保存设置
section.SectionInformation.ForceSave = true;
//配置文件内容保存到xml
config.Save(ConfigurationSaveMode.Full);
}