zoukankan      html  css  js  c++  java
  • 修改 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     }
  • 相关阅读:
    POJ 2175 Evacuation Plan 费用流 负圈定理
    POJ 2983 Is the Information Reliable? 差分约束
    codeforces 420B Online Meeting
    POJ 3181 Dollar Dayz DP
    POJ Ant Counting DP
    POJ 1742 Coins DP 01背包
    中国儒学史
    产品思维30讲
    Java多线程编程核心技术
    编写高质量代码:改善Java程序的151个建议
  • 原文地址:https://www.cnblogs.com/z5337/p/3676782.html
Copyright © 2011-2022 走看看