基于动态修改App.Config与web.Config的使用详解
时间:2021-07-19 12:09:27
首先假设你的应用程序配置文件如下:
<?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节点,然后修改,接着保存。
运行:结果如下:
可以看到输出的值是两个old.
为什么??
查找msdn文档可以发现微软出于性能考虑,对ConfigurationManager采用了缓存策略,所以如果要读取新的值,应该使用ConfigurationManager的RefreshSection来进行刷新,
ConfigurationManager . RefreshSection:
刷新命名节,这样在下次检索它时将从磁盘重新读取它。
于是将Main方法修改为:
Console.WriteLine(ConfigurationManager.AppSettings["name"]);
ChangeConfiguration();
ConfigurationManager.RefreshSection("appSettings");
Console.WriteLine(ConfigurationManager.AppSettings["name"]);
重新清理解决方案,重新运行:
可以看到,仍然是两个old。。。
为什么??
难道值没有修改??,我们打开应用程序的配置文件,可以通过监视assemblyConfigFile获得路径
上面是xxx\bin\Debug\CAStudy.exe.,对应的配置文件就是CAStudy.exe.config
文件的内容如下:
可以发现value 值已经更改,那么为什么输出还是old,old 呢??
为了验证不是VS2010的问题。
首先手动将CAStudy.exe.config 文件中的value改为”old”,接着再次运行CAStudy.exe 结果如下:
可以看到输出时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;
重新运行,结果如下:
另外值得一提的是: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");
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java日期操作类常见用法示例
unity实现场景切换进度条显示
![](https://img.aspxhome.com/file/2023/5/104305_0s.jpg)
Java中JUC 的 Exchange 交换器详情
![](https://img.aspxhome.com/file/2023/2/76252_0s.webp)
Java 8 开发的 Mybatis 注解代码生成工具
![](https://img.aspxhome.com/file/2023/1/66211_0s.png)
MyEclipse设置Console输出到文件的实现方法
![](https://img.aspxhome.com/file/2023/3/73023_0s.png)
java网络编程基础知识介绍
![](https://img.aspxhome.com/file/2023/7/77987_0s.jpg)
java实现字符串和数字转换工具
搞懂Java线程池
![](https://img.aspxhome.com/file/2023/5/62345_0s.jpg)
Java多线程(单例模式,堵塞队列,定时器)详解
![](https://img.aspxhome.com/file/2023/9/100349_0s.png)
java IO流 之 输入流 InputString()的使用
SpringBoot中的HATEOAS详情
Android编程实现滑动按钮功能详解
![](https://img.aspxhome.com/file/2023/7/108317_0s.png)
Java中static变量作用和用法详解
idea 实现搜索jdk中的类和包操作
![](https://img.aspxhome.com/file/2023/9/71169_0s.jpg)
图解Eclipse在线安装ADT插件过程
![](https://img.aspxhome.com/file/2023/8/101818_0s.png)
C#实现HSL颜色值转换为RGB的方法
Android Studio 3.6 layout文件text模式切换问题
![](https://img.aspxhome.com/file/2023/5/108255_0s.png)
C# 时间与时间戳互转的方法(13位)
基于java 线程的几种状态(详解)
![](https://img.aspxhome.com/file/2023/5/61845_0s.png)
SpringBoot项目部署到腾讯云的实现步骤
![](https://img.aspxhome.com/file/2023/7/70557_0s.png)