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     }
  • 相关阅读:
    将SL4 程序移植到WP7(附Teched2010“.NET研究”真机图) 狼人:
    WP7有约(二“.NET研究”):课后作业 狼人:
    Android平台SQLite快速入门“.NET研究”实践 狼人:
    移动Web界面构建最佳“.NET研究”实践 狼人:
    Androi“.NET研究”d如何在三年时间里征服移动世界的 狼人:
    详解如何让Android UI“.NET研究”设计性能更高效 狼人:
    Android 2.3预计下周发布 十大惊“.NET研究”喜不容错过 狼人:
    Android的移动存储解决方案“.NET研究”之SharedPreferences 狼人:
    C++开发者快速学习ObjectiveC语言核“.NET研究”心语法 狼人:
    图像像素OpenCV4Android一点关于图像的基础
  • 原文地址:https://www.cnblogs.com/z5337/p/3676782.html
Copyright © 2011-2022 走看看