c# - 循环遍历 appConfig 文件的不同部分

标签 c# app-config

我有一个 AppConfig 文件,如下所示。我试图遍历配置部分并获取部分名称,基于部分名称,它应该选择适当的 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;
                }
            }
        }

感谢任何帮助!!

最佳答案

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

这里有几个例子:

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

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


阅读评论后,我认为这更符合您的要求。我忘记了完全不直观的配置部分。

<?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  

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

关于c# - 循环遍历 appConfig 文件的不同部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37028994/

相关文章:

c# - 显示常见 EF 删除语句与引用约束冲突的友好错误

c# - App.config 弃用了 C#?

c# - 引号内的 XML 引号

c# - C#-与设计有关的配置问题(两个)

c# - 我将如何对这个 nhibernate 查询进行单元测试?

C#:如何创建一个只接受 0 或更高整数值的程序?

c# - MonoTouch 的 QR 库?

c# - 是否可以在运行时切换 app.config?

.net - 如何在 ConfigurationSection 中包含简单的集合

c# - 如何以编程方式修改 WCF app.config 端点地址设置?