C# 程序在app.config中找不到Sectiongroup
我正在开发一个控制台程序,它应该从我定义的app.config中找到一个sectiongroup,并在foreach循环中获取每个sections键。 这是我的app.config:C# 程序在app.config中找不到Sectiongroup,c#,.net,app-config,C#,.net,App Config,我正在开发一个控制台程序,它应该从我定义的app.config中找到一个sectiongroup,并在foreach循环中获取每个sections键。 这是我的app.config: <?xml version="1.0"?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> </start
<?xml version="1.0"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<appSettings>
</appSettings>
<configSections>
<sectionGroup name="JobCheckerConfigGroup">
<section name="TheJob" type="System.Configuration.NameValueSectionHandler"/>
<section name="TheOtherJob" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
<JobCheckerConfigGroup>
<TheJob>
<add key="Path" value="PathToTheJob" />
<add key="JobName" value="TheJob" />
</TheJob>
<TheOtherJob>
<add key="Path" value="*PathToTheOtherJob" />
<add key="JobName" value="TheOtherJob" />
</TheOtherJob>
</JobCheckerConfigGroup>
</configuration>
这段代码在第一个if处结束,但有一个异常,即:参数sectionGroupName不存在
另一方面,我尝试:
foreach (ConfigurationSectionGroup sectionGroup in sectionGroups)
{
if (sectionGroup.Name == "JobCheckerConfigGroup")
{
foreach (ConfigurationSection configSection in sectionGroup.Sections)
{
var section = ConfigurationManager.GetSection(configSection.SectionInformation.SectionName) as NameValueCollection;
myPath = section["Path"];
}
}
}
在这段代码中,程序会找到其他十个分区组,但不会找到我的JobCheckerConfigGroup
我试着按照这个指示去做:但我无法让它为我工作。
有人能解决这个问题吗?
谢谢。IIRC,配置部分必须是配置部分的第一部分。我似乎在所有教程中都跳过了这一部分,现在可以使用了。非常感谢。
foreach (ConfigurationSectionGroup sectionGroup in sectionGroups)
{
if (sectionGroup.Name == "JobCheckerConfigGroup")
{
foreach (ConfigurationSection configSection in sectionGroup.Sections)
{
var section = ConfigurationManager.GetSection(configSection.SectionInformation.SectionName) as NameValueCollection;
myPath = section["Path"];
}
}
}