【发布时间】: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