Skip to main content
 首页 » 编程设计

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

2024年02月05日32Free-Thinker

我有一个 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   

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