【问题标题】:C# - override configuration manuallyC# - 手动覆盖配置
【发布时间】:2020-05-27 23:40:46
【问题描述】:

我正在使用 C# .net

我需要加载一个外部配置文件 (web.config) 并覆盖当前配置中所有部分和所有元素(每个部分)的值。

即覆盖后的状态应该是:

ConfigurationManager.AppSettings = ...从 web.config 读取的应用设置...

所有部分都以此类推。

在类似代码(或更简单的代码,如果有的话)中。

string s = "c:\test\web.config";

Configuration config = ConfigurationManager.OpenExeConfiguration(s);
foreach(ConfigurationSection section in config.sections) {
  // **** What to do here in order to do: ConfigurationManager.Override(section)
  // so, I can use, i.e, line code such as : string s = ConfigurationManager.AppSettings["key"] ?
}

谢谢。

【问题讨论】:

    标签: c# xml configuration


    【解决方案1】:

    我找到了解决方案,我想参与其中。

    代码如下:

    // For the file name:
    string s = "c:\test\web.config";
    
    // Now creating configuration:
    Configuration roamingConfig =
        ConfigurationManager.OpenExeConfiguration(s);
    
    // configure the map
    ExeConfigurationFileMap configFileMap =
        new ExeConfigurationFileMap();
    
    configFileMap.ExeConfigFileName = s;
    
    // Now add new configuration, based on the configuration file map.
    Configuration configFile = 
        ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
    
    var allKeys = configFile.AppSettings.Settings.AllKeys;
    foreach (var key in allKeys) {
      ConfigurationManager.AppSettings["key"] = 
         configFile.AppSettings.Settings[key].value;
    }
    
    configFile.SaveAs(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
    ConfigurationManager.RefreshSection("appSettings");
    

    我简单的做的是两次声明配置(第二次是用map exe配置打开的),将文件保存到它的本机执行路径,然后刷新配置部分appSettings。

    【讨论】:

      【解决方案2】:

      一种方法是首先使用SaveAs 函数覆盖应用程序的配置文件。然后使用ConfigurationManager.RefreshSection 函数刷新所有配置部分。

      选项 1:借助 RefreshSection

      string s = "c:\test\web.config";
      
      var configFile = ConfigurationManager.OpenExeConfiguration(s);
      
      //Overwrite config file of app
      configFile.SaveAs(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
      
      //Iterate through each sections 
      foreach(ConfigurationSection section in configFile.Sections)
      {
          //Refresh each section
          ConfigurationManager.RefreshSection(section.SectionInformation.Name);
      }
      

      [编辑]

      选项 2:添加示例以使用从不同位置的配置读取的值更新 key-value 对 AppSettings 部分。

      string s = "c:\test\web.config";
      var configFile = ConfigurationManager.OpenExeConfiguration(s);
      //Get all keys
      var allKeys = configFile.AppSettings.Settings.AllKeys;
      foreach (var key in allKeys)
      {
          // Set value in ConfigurationManager
          ConfigurationManager.AppSettings["Name"] = 
                                     configFile.AppSettings.Settings[key].Value;
      }
      

      【讨论】:

      • 这对我来说实际上不适用于单元测试,我需要将库的方法与它自己的配置文件一起使用。该项目是单元测试(ms-unit),我收到错误:文件 c:\program files (x86)\Microsoft visual studion\2019\Proffessional\Common7\IDE\Extensions\TestPlatform\ir0w25of.tmp 被拒绝。在当前配置文件的地方,我的意思是: AppDomain.CurrentDomain.SetupInformation.ConfigurationFile 的值: c:\program files (x86)\Microsoft visual studion\2019\Proffessional\Common7\IDE\Extensions\TestPlatform\testhost.x86.exe .Config.
      • 许多键(配置文件中的所有键,可能会有所不同)。
      • @Eitan 好的。在这种情况下,使用SaveAs 可能不是优化选项,因为它会多次触发RefreshSection。也许从configFile 值循环更新ConfigurationManager.AppSettings 会更好。
      • 我该怎么做?我知道我加载了正确的配置 xml 文件,但是我如何无法解析部分内的值(即使在调试器中,我也没有成功。在循环 foreach ... if (section.name == "appSettings" 那么什么??)。
      • 感谢您的努力(我将您的答案标记为有用,因为您为我提供了如何解决问题的好线索)。经过一番努力,我找到的解决方案如下。
      猜你喜欢
      • 2013-11-26
      • 1970-01-01
      • 1970-01-01
      • 2011-09-24
      • 1970-01-01
      • 2017-09-08
      • 2014-07-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多