动态修改App.Config 和web.Config

简介:

首先假设你的应用程序配置文件如下:

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <appSettings>

    <add key="name" value="old"/>

  </appSettings>

</configuration>

 

Ok,那么如何在运行时去修改name的值呢??

有很多童鞋会说可以使用Xml读取配置文件,然后xxx。。。。

当然这种方法肯定可以解决问题,有没有其他方法呢??

在这里我要介绍一种比较简单的方法,可能已经有人知道了,那就是使用ConfigurationManager

ConfigurationManager 存在System.Configuration.dll 中。

代码如下:

public static void Main()
{
    Console.WriteLine(ConfigurationManager.AppSettings["name"]);
    ChangeConfiguration();
    Console.WriteLine(ConfigurationManager.AppSettings["name"]);
    Console.ReadLine();
}

private static void ChangeConfiguration()
{
    //读取程序集的配置文件
    string assemblyConfigFile = Assembly.GetEntryAssembly().Location;

    Configuration config = ConfigurationManager.OpenExeConfiguration(assemblyConfigFile);
    //获取appSettings节点
    AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
    
    //删除name,然后添加新值
    appSettings.Settings.Remove("name");
    appSettings.Settings.Add("name", "new");

    //保存配置文件
    config.Save();
}

代码很简单:首先读取配置文件,接着获取appSettings节点,然后修改,接着保存。

运行:结果如下:

clip_image002

可以看到输出的值是两个old.

为什么??

查找msdn文档可以发现微软出于性能考虑,对ConfigurationManager采用了缓存策略,所以如果要读取新的值,应该使用ConfigurationManagerRefreshSection来进行刷新,

ConfigurationManager . RefreshSection:

刷新命名节,这样在下次检索它时将从磁盘重新读取它。

于是将Main方法修改为:

Console.WriteLine(ConfigurationManager.AppSettings["name"]);

ChangeConfiguration();

ConfigurationManager.RefreshSection("appSettings");

Console.WriteLine(ConfigurationManager.AppSettings["name"]);

重新清理解决方案,重新运行:

clip_image004

可以看到,仍然是两个old。。。

为什么??  

难道值没有修改??,我们打开应用程序的配置文件,可以通过监视assemblyConfigFile获得路径

上面是xxx\bin\Debug\CAStudy.exe.,对应的配置文件就是CAStudy.exe.config

clip_image006

文件的内容如下:

clip_image008

 

可以发现value 值已经更改,那么为什么输出还是old,old 呢??

为了验证不是VS2010的问题。

首先手动将CAStudy.exe.config 文件中的value改为”old”,接着再次运行CAStudy.exe 结果如下:

clip_image010

 

可以看到输出时old,和new。为什么会这样???

难道调试时读取的不是修改的配置文件,或者修改的配置文件并不是调试的应用程序读取的文件??

assemblyConfigFile 中设置断点,可以发现assemblyConfigFile 读取的是CAStudy.exe.Config。但是vs调试的时候运行的是CAStudy.vshost.exe。也就是说我们使用ConfigurationManager.OpenExeConfiguration 打开的是CAStudy.exe.config文件,但是我们调试的应用程序CAStudy.vshost.exe使用的是CAStudy.vshost.exe.config文件。

那么还有其他的方式可以准确的获取应用程序配置文件吗??

有的,使用AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

ChangeConfiguration()方法修改如下:

private static void ChangeConfiguration()
{

    //读取程序集的配置文件
    string assemblyConfigFile = Assembly.GetEntryAssembly().Location;
    string appDomainConfigFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    
     //获取appSettings节点
    AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
    
    //删除name,然后添加新值
    appSettings.Settings.Remove("name");
    appSettings.Settings.Add("name", "new");

    //保存配置文件
    config.Save();
}

清理,重新运行:

使用默认的不传递字符串的版本就可以打开当前配置文件了。

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

如果要查看当前配置文件的完整路径可以使用AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

 

重新运行,结果如下:

clip_image012

 

另外值得一提的是:ConfigurationManager.RefreshSection 不支持section Group.所以对于WCF的服务,你必须一个一个的RefreshSection:

 

ConfigurationManager.RefreshSection("system.serviceModel/behaviors");

ConfigurationManager.RefreshSection("system.serviceModel/bindings");

ConfigurationManager.RefreshSection("system.serviceModel/client");

ConfigurationManager.RefreshSection("system.serviceModel/services");

参考文章:How to Change .NET Configuration Files at Runtime (including for WCF)






本文转自LoveJenny博客园博客,原文链接:http://www.cnblogs.com/LoveJenny/archive/2011/07/11/2103419.html,如需转载请自行联系原作者
目录
相关文章
|
4月前
【Azure App Service】PowerShell脚本批量添加IP地址到Web App允许访问IP列表中
Web App取消公网访问后,只允许特定IP能访问Web App。需要写一下段PowerShell脚本,批量添加IP到Web App的允许访问IP列表里!
|
7月前
|
Shell PHP Windows
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
|
7月前
|
Linux 应用服务中间件 网络安全
【Azure 应用服务】查看App Service for Linux上部署PHP 7.4 和 8.0时,所使用的WEB服务器是什么?
【Azure 应用服务】查看App Service for Linux上部署PHP 7.4 和 8.0时,所使用的WEB服务器是什么?
|
7月前
【Azure 应用服务】通过 Web.config 开启 dotnet 应用的 stdoutLog 日志,查看App Service 产生500错误的原因
【Azure 应用服务】通过 Web.config 开启 dotnet 应用的 stdoutLog 日志,查看App Service 产生500错误的原因
101 0
|
7月前
|
Linux Python
【Azure 应用服务】Azure App Service For Linux 上实现 Python Flask Web Socket 项目 Http/Https
【Azure 应用服务】Azure App Service For Linux 上实现 Python Flask Web Socket 项目 Http/Https
|
7月前
|
存储 安全 网络安全
【Azure 环境】使用Azure中的App Service部署Web应用,以Windows为主机系统是否可以启动防病毒,防恶意软件服务呢(Microsoft Antimalware)?
【Azure 环境】使用Azure中的App Service部署Web应用,以Windows为主机系统是否可以启动防病毒,防恶意软件服务呢(Microsoft Antimalware)?
|
7月前
|
存储 Linux 网络安全
【Azure 应用服务】App Service For Linux 如何在 Web 应用实例上住抓取网络日志
【Azure 应用服务】App Service For Linux 如何在 Web 应用实例上住抓取网络日志
|
7月前
|
Web App开发 安全 JavaScript
【Azure 应用服务】App Service 通过配置web.config来添加请求返回的响应头(Response Header)
【Azure 应用服务】App Service 通过配置web.config来添加请求返回的响应头(Response Header)
117 0
|
7月前
|
监控 Java 应用服务中间件
【Azure 应用服务】App Service中抓取 Web Job 的 DUMP 办法
【Azure 应用服务】App Service中抓取 Web Job 的 DUMP 办法
|
7月前
|
Linux 网络安全 容器
【Azure App Service for Linux】Linux Web App如何安装系统未安装的包
【Azure App Service for Linux】Linux Web App如何安装系统未安装的包

热门文章

最新文章