0

I´m working on a console program, which is supposed to find a sectiongroup from my app.config that i defined and get every sections keys in a foreach loop. Here is my app.config:

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

This is the C# Code:

static void Main(string[] args)
        {
            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            ConfigurationSectionGroupCollection sectionGroups = config.SectionGroups;

            if (sectionGroups["JobCheckerConfigGroup"] != null)
            {
                foreach (ConfigurationSection mySection in sectionGroups["JobCheckerConfigGroup"].Sections)
                {
                    NameValueCollection db = (NameValueCollection)ConfigurationManager.GetSection("JobCheckerConfigGroup/" + mySection.SectionInformation.Name);
                        //ConfigurationSettings.GetConfig("JobCheckerConfigGroup/" + mySection.SectionInformation.Name);
                }
            }
        }

This piece of code ends at the first "if" with an exception, that says: "The Parameter sectionGroupName does not exist"

The other way I tried:

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"];
                    }
                }
            }

In this code, the program finds ten other sectiongroups but not my "JobCheckerConfigGroup" I tried to follow this instruction: How do you use sections in c# 4.0 app.config? but i can´t get it working for me. Does any one have a solution for this? Thanks.

Community
  • 1
  • 1
SteffND
  • 1
  • 1

0 Answers0