修改 App.Config 配置文件 C#
今天在个WCF程序中加入了修改配置文件的功能。我是直接通过IO操作修改的app.config文件内容,修改后发现发现其并不生效,用Google搜了一下,在园子里的文章动态修改App.Config 和web.Config中找到了解决方案。
原来,.net framework中对于配置文件不是实时读取的,而是有缓存的。对于那些已经更新了的内容,需要调用ConfigurationManager.RefreshSection(需要添加System.Configuration.dll的引用)函数刷新相应节点。
比较蛋疼的是,这个函数并不支持刷新Group。也就是说,我们不能通过ConfigurationManager.RefreshSection("system.serviceModel")一句话实现对WCF的配置刷新,需要调用如下四句话才行。
ConfigurationManager.RefreshSection("system.serviceModel/behaviors");
ConfigurationManager.RefreshSection("system.serviceModel/bindings");
ConfigurationManager.RefreshSection("system.serviceModel/client");
ConfigurationManager.RefreshSection("system.serviceModel/services");
另外,值得一提的是:如果用IO操作修改修改app.config配置,直接使用相对路径"myapp.exe.config"来修改不可靠的,很容易出现找不到配置文件的异常(原因有很多种),需要使用AppDomain.CurrentDomain.SetupInformation.ConfigurationFile属性来获取配置文件的完整路径。
我一开始在这两个网站找到的提示
http://developer.51cto.com/art/200908/146303.htm
http://www.codeproject.com/Articles/14744/Read-Write-App-Config-File-with-NET
结果调试结果不太稳定, 修改了好几次,代码如下
public class RWConfig
{
/// <summary>
/// 读 ConnectionStrings 节点 ConnectionName 的连接字符串
/// 节点不存在时返回 null
/// </summary>
public static string GetConnectionStringConfig(string ConnectionName)
{
if (ConfigurationManager.ConnectionStrings[ConnectionName] == null)
return null;
return ConfigurationManager.ConnectionStrings[ConnectionName].ConnectionString.ToString();
} /// <summary>
/// 获得指定配置节点的值
/// 节点不存在时返回 null
/// </summary>
public static string GetAppConfig(string strKey)
{
if (ConfigurationManager.AppSettings[strKey] == null)
return null;
return ConfigurationManager.AppSettings[strKey].ToString();
} // 写 Config 不稳定
public static void AddConnectionStringConfig(string newName, string newConString, string newProvideName)
{
// 新建一个连接字符串
ConnectionStringSettings mySettings = new ConnectionStringSettings(newName, newConString, 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(mySettings);
// 保存对配置文件的更改
config.Save(ConfigurationSaveMode.Minimal);
ConfigurationManager.RefreshSection("connectionStrings");
} public static void AddAppConfig(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");
}
}
修改 App.Config 配置文件 C#的更多相关文章
- 【C#】#103 动态修改App.config配置文件
对 C/S模式 下的 App.config 配置文件的AppSetting节点,支持配置信息现改现用,并可以持久保存. 一. 先了解一下如何获取 配置信息里面的内容[获取配置信息推荐使用这个] 1.1 ...
- 在WCF程序中动态修改app.config配置文件
今天在个WCF程序中加入了修改配置文件的功能.我是直接通过IO操作修改的app.config文件内容,修改后发现发现其并不生效,用Google搜了一下,在园子里的文章动态修改App.Config 和w ...
- C# 读写App.config配置文件的方法
我们经常会希望在程序中写入一些配置信息,例如版本号,以及数据库的连接字符串等.你可能知道在WinForm应用程序中可以利用Properties.Settings来进行类似的工作,但这些其实都利用了Ap ...
- C#中动态读写App.config配置文件
转自:http://blog.csdn.net/taoyinzhou/article/details/1906996 app.config 修改后,如果使用cofnigurationManager立即 ...
- C#项目实例中读取并修改App.config文件
C#项目是指一系列独特的.复杂的并相互关联的活动,这些活动有着一个明确的目标或目的,必须在特定的时间.预算.资源限定内,依据规范完成.项目参数包括项目范围.质量.成本.时间.资源. 1. 向C#项目实 ...
- 关于App.config配置文件
今天在做复习的时候,突然发现自己无法读取配置文件中的数据库连接字符串,而且检查了半天也没找出原因,最后求助万能的度娘才得以解决—— 1.App.config配置文件在项目中添加后不要修改名称,否则会出 ...
- C# 读写App.config配置文件
一.C#项目中添加App.config配置文件 在控制台程序中,默认会有一个App.config配置文件,如果不小心删除掉,或者其他程序需要配置文件,可以通过添加得到. 添加步骤:右键项目名称,选择“ ...
- 读写App.config配置文件的方法
我们经常会希望在程序中写入一些配置信息,例如版本号,以及数据库的连接字符串等.你可能知道在WinForm应用程序中可以利用Properties.Settings来进行类似的工作,但这些其实都利用了Ap ...
- VS动态修改App.config中遇到的坑(宿主进程问题)
昨天遇到了很奇怪的一个bug,具体描述如下: 这个系统是c/s架构的针对多个工厂做的资材管理系统,由于有很多个工厂,每个工厂都有自己的服务器.所以需要动态的改变连接字符串去链接不同的服务器. 由于这个 ...
随机推荐
- UE4 IOS 开发之传感器输入
Iphone的传感器包括陀螺仪.加速计等. UE4提供了4个按键来收集这些传感器的3维数据.具体位置:ProjectSettings->Input. 其中Tilt数据可以反映Iphone目前的物 ...
- hdu4059 The Boss on Mars 容斥原理
On Mars, there is a huge company called ACM (A huge Company on Mars), and it’s owned by a younger bo ...
- hdu2461 Rectangles 线段树--扫描线
You are developing a software for painting rectangles on the screen. The software supports drawing s ...
- Redis(一)入门
最近,学习了一下,Redis 这个Nosql数据库,从安装到基本语法,作为入门.下面,整理一下基本知识. 参考的地址如下: http://www.runoob.com/redis/redis-java ...
- codeforces 788A Functions again
…… 原题: Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandia ...
- 【shell编程】之基础知识-常用命令
一.Shell echo命令 Shell 的 echo 指令与 PHP 的 echo 指令类似,都是用于字符串的输出.命令格式: echo string 您可以使用echo实现更复杂的输出格式控制. ...
- react 简书开发笔记
详见文章<React简书开发实战课程笔记>
- dc-vastinspector
https://developers.google.com/interactive-media-ads/docs/sdks/html5/vastinspector hosts: https://gis ...
- mysql之主从配置实现
我使用的是两台centos7虚拟机来做实验的,主服务器ip为192.168.2.128,从服务器ip为192.168.2.130 安装mysql就不用说了吧,不对,我们需要安装的是mariadb,命令 ...
- Server 2008 R2部署active directory服务器-ad域
一.部署AD域: 系统环境:Windows server 2008 R2 标准版 ip:172.16.1.149服务器开机后会自动弹出一个初始配置任务窗口(这个不要随便关闭,因为我不知道关闭后能在哪里 ...