修改 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

结果调试结果不太稳定, 修改了好几次,代码如下 

 1     public class RWConfig
 2     {
 3         /// <summary>
 4         /// 读 ConnectionStrings 节点 ConnectionName 的连接字符串
 5         /// 节点不存在时返回 null
 6         /// </summary>
 7         public static string GetConnectionStringConfig(string ConnectionName)
 8         {
 9             if (ConfigurationManager.ConnectionStrings[ConnectionName] == null)
10                 return null;
11             return ConfigurationManager.ConnectionStrings[ConnectionName].ConnectionString.ToString();
12         }
13 
14         /// <summary>
15         /// 获得指定配置节点的值
16         /// 节点不存在时返回 null
17         /// </summary>
18         public static string GetAppConfig(string strKey)
19         {
20             if (ConfigurationManager.AppSettings[strKey] == null)
21                 return null;
22             return ConfigurationManager.AppSettings[strKey].ToString();
23         }
24 
25         // 写 Config 不稳定
26         public static void AddConnectionStringConfig(string newName, string newConString, string newProvideName)
27         {
28             // 新建一个连接字符串
29             ConnectionStringSettings mySettings = new ConnectionStringSettings(newName, newConString, newProvideName);
30             // 打开配置文件
31             //Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
32             Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
33             if (ConfigurationManager.ConnectionStrings[newName] != null)
34                 config.ConnectionStrings.ConnectionStrings.Remove(newName);
35             // 添加新的连接字符串
36             config.ConnectionStrings.ConnectionStrings.Add(mySettings);
37             // 保存对配置文件的更改
38             config.Save(ConfigurationSaveMode.Minimal);
39             ConfigurationManager.RefreshSection("connectionStrings");
40         }
41 
42         public static void AddAppConfig(string newKey, string newValue)
43         {
44             //Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
45             Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
46             if (ConfigurationManager.AppSettings[newKey] != null)
47                 config.AppSettings.Settings.Remove(newKey);
48             config.AppSettings.Settings.Add(newKey, newValue);
49             config.Save(ConfigurationSaveMode.Minimal);
50             ConfigurationManager.RefreshSection("appSettings");
51         }
52     }

 打开某个端口的授权命令:

netsh http add urlacl url=http://+:8000/ user=Domain\UserName

 

1.入站规则

netsh advfirewall firewall add rule name="SGAccessInboundRule" dir=in protocol=tcp action=allow localport=8061-8080,18061-180802.出站规则

netsh advfirewall firewall add rule name="SGAccessOutboundRule" dir=out protocol=tcp action=allow remoteport=8061-8080,18061-18080
-----------------------------------
Windows批量添加防火墙例外端口
https://blog.51cto.com/u_15072780/4648805

 

posted on 2014-04-20 18:16  z5337  阅读(2927)  评论(0编辑  收藏  举报