4

I've set up some custom config sections in my App.Config, such that I now have a configSection that looks like this.

<configSections>
    <section name="Section1" type="ConfigSections.MySection, MyNamespace"/>    
    <section name="Section2" type="ConfigSections.MySection, MyNamespace"/>    
    <section name="Section3" type="ConfigSections.MySection, MyNamespace"/>    
</configSections>

What I want to do is to read this section in code in order to find out at runtime what sections I have. I have tried:

var mySections = ConfigurationManager.GetSection("configSections");

but this returns null. I'm sure I'm missing something simple, but I cannot find anything about how to do this.

Thanks

3
  • It think you have to do it like this: var mySections = ConfigurationManager.GetSection("Section1"); Commented Apr 26, 2012 at 10:47
  • To clarify, I am perfectly happy how to read the section called "Section1". I am trying to read the section called "configSections", so I don't have to hardcode the text "Section1" in my code. The reason for this is that I don't know at runtime what or how many sections I'm going to have. Commented Apr 26, 2012 at 11:13
  • To put it another way, I want to be able to read the "name" attribute, on each of the "section" elements, contained in the "configSections" element (above), in my App.Config. (This is hard to explain!) Commented Apr 26, 2012 at 11:15

2 Answers 2

6

Use the Configuration.Sections-property to get the names of the declared configuration sections. Then, optionally if you need, use ConfigurationManager.GetSection() to retrieve an individual section.

Note that you may want to use the value of the SectionInformation.IsDeclared or ConfigSource of the respective ConfigurationSection.SectionInformation to find out of the section was actually declared in your configuration file, or is inherited from machine.config or otherwise.

Example:

    var cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    var localSections = cfg.Sections.Cast<ConfigurationSection>()
       .Where(s => s.SectionInformation.IsDeclared);

Finally, note that this approach will only get you configuration sections. It will not return configuration sections, which are itself inside a <sectionGroup>. For them, you would first need iterate over Configuration.SectionGroups, which has it's own Sections-property that contains the per section group sections. It can also contain nested section groups, again accessible via the SectionGroups property of each ConfigurationSectionGroup instance.

Sign up to request clarification or add additional context in comments.

2 Comments

Thankyou. That appears to be exactly what I need. Cheers!
I guess the first sentence of the last paragraph is trying to say, "This approach will get you root-level configuration sections, but not configuration sections which are nested inside a <sectionGroup>."
0

if put all the sections into a section group this would work:

<configSections>
      <sectionGroup name="FMGlobal.Common.SecuritySubsystem.ADAzManFeed">
        <section name="ADFolders" type="System.Configuration.NameValueSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </sectionGroup>
    </configSections>

  var  NVC = (ConfigurationManager.GetSection( _
    "FMGlobal.Common.SecuritySubsystem.ADAzManFeed")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.