【问题标题】:Loop through different sections of appConfig file循环遍历 appConfig 文件的不同部分
【发布时间】:2016-08-29 23:11:17
【问题描述】:

我有一个如下所示的 AppConfig 文件。我正在尝试遍历 configsections 并获取部分名称,根据部分名称,它应该选择适当的 appSettings。例如,当第一部分是 VehicleConfig 时,它应该自动选择 VehicleConfig 的 appSettings。我需要这种自动选择,因为我有多个部分,我必须根据其部分名称获取不同部分的 appSettings。

    <configuration>
     <configSections>
      <sectionGroup name = "mySection">
       <section name="VehicleConfig"/>
       <section name="LiveDownloaderConfig"/>
      </sectionGroup>
     </configSections>
     <VehicleConfig>
       <appSettings>
         <add key="FileRoot" value="C:\FilesToUpload" />
         <add key="Project" value="BigDataTest" />
         <add key="Dataset" value="StoreServer" />
       </appSettings>
     </VehicleConfig>
     <LiveDownloaderConfig>
       <appSettings>
        <add key="FileRoot" value="C:\FilesToUpload" />
        <add key="Project" value="BigDataTest" />
        <add key="Dataset" value="BQSMeasure" />
       </appSettings>
     </LiveDownloaderConfig>
  </configuration>

我已经尝试过这段代码,当第二个 for-each 循环被命中时,它会抛出错误“无法识别的元素 appSettings inside VehicleConfig”。我尝试删除 appSettings,但随后它抛出“无法识别的元素添加”。我想知道我是否可以在 VehicleConfig 中包含这些元素。

 System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

 ConfigurationSectionGroupCollection sectionGroups = config.SectionGroups;

         foreach (ConfigurationSectionGroup group in sectionGroups)
        // Loop over all groups
        {
            Console.WriteLine(group);
            if (group.Name == "FileCheckerConfigGroup")
            {
                foreach (ConfigurationSection configurationSection in group.Sections)
                {
                    var section = ConfigurationManager.GetSection(configurationSection.SectionInformation.SectionName) as NameValueCollection;
                }
            }
        }

感谢任何帮助!

【问题讨论】:

    标签: c# app-config


    【解决方案1】:

    通常我们通过ConfigurationManager 到达Configuration。但在这种情况下,您需要直接访问Configuration 对象。这有点令人讨厌,因为您访问它的方式因 Web 应用程序和其他应用程序而异。

    以下是一些示例:

    var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
    var config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/")
    

    一旦您引用了该对象,您就可以使用它的.SectionGroups.Sections 属性来遍历各个部分。


    在阅读了 cmets 之后,我认为这就是您要寻找的更具体的内容。我忘记了配置部分是多么不直观。

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <configSections>
        <section name="mySettings" type="System.Configuration.AppSettingsSection"/>
        <section name="myOtherSettings" type="System.Configuration.AppSettingsSection"/>
      </configSections>
      <mySettings>
        <add key="foo" value="bar"/>
      </mySettings>
      <myOtherSettings>
        <add key="yourUncle" value="bob" />
      </myOtherSettings>
    </configuration>
    

    然后阅读它们,

    class Program
    {
        static void Main(string[] args)
        {
            var configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            foreach (var sectionKey in configuration.Sections.Keys)
            {
                var section = configuration.GetSection(sectionKey.ToString());
                var appSettings = section as AppSettingsSection;
                if (appSettings == null) continue;
                Console.WriteLine(sectionKey);
                foreach (var key in appSettings.Settings.AllKeys)
                {
                    Console.WriteLine("  {0}: {1}", key, appSettings.Settings[key].Value);
                }
            }
            Console.ReadLine();
        }
    }
    

    输出:

    appSettings  
    myOtherSettings    
      yourUncle: bob  
    mySettings   
      foo: bar  
    

    你需要添加一些额外的东西来迭代嵌套配置部分,但我不能让你没有任何乐趣。

    【讨论】:

    • 我试过这个代码。即使我已经声明了我的部分组,它仍然说该组未声明。
    • 您还需要为配置部分指定一个类型,以便理解如何阅读它。在上面的示例中,从部分中删除 &lt;appSettings&gt;,并将其添加到您的 &lt;section&gt; 声明中:type="System.Configuration.AppSettingsSection, System.Configuration"
    • foreach(sectionGroups 中的 ConfigurationSectionGroup 组){ Console.WriteLine(group); if (group.Name == "FileCheckerConfigGroup") { foreach (ConfigurationSection configurationSection in group.Sections) { var section = ConfigurationManager.GetSection(configurationSection.SectionInformation.SectionName) as NameValueCollection; } } }
    【解决方案2】:

    请试试这个

    foreach (var key in System.Configuration.ConfigurationSettings.AppSettings)
    {
    
    }
    

    【讨论】:

    • 仅循环通过配置文件的 appsettings 部分中的设置。
    【解决方案3】:

    简单的方法是将文件视为普通 XML 文件,也许使用 XDocument,然后您可以使用 linq 查询它...

    var config = XDocument.Load(File.OpenRead("app.config"));
    var sections = config.Descendants("configuration");
    
    foreach(var section in sections) {
        // stuff
    }
    

    要获得特定部分的设置,您需要使用更深的 xpath 表达式...

    var sectionName = "VehicleConfig";
    var settings = config.Descendants("configuration/" + sectionName + "/appSettings");
    

    然后,您可以使用上述简单的 foreach 遍历这些设置

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-20
      • 2019-09-03
      • 2021-04-21
      • 2012-07-10
      • 1970-01-01
      • 2015-02-02
      • 2014-04-10
      相关资源
      最近更新 更多