65

I want to use my app config to store the settings for 2 companys, and i'd prefer if it was possible to use a section to seperate the data for one from the other rather then giving them diffrent key names.

I have been checking online but i seem to get a bit overwhelmed when people use sections or find outdated easy ways to use them. could anyone pass me a beginner guide on them?

Below is an example of what my app.config would look like:

  <configSections>
    <section name="FBI" type="" />
    <section name="FSCS" type="" />
  </configSections>

  <FSCS>
    <add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>
  </FSCS>
  <FBI>
    <add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>
  </FBI>

Update:

Advanced solution based on the anwer. in case anyone wanted to know.

App.config:

<configuration>
    <configSections>
        <sectionGroup name="FileCheckerConfigGroup">
          <section name="FBI" type="System.Configuration.NameValueSectionHandler" />
          <section name="FSCS" type="System.Configuration.NameValueSectionHandler" />
        </sectionGroup>
    </configSections>
    <FileCheckerConfigGroup>
        <FSCS>
            <add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>
        </FSCS>
        <FBI>
            <add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>
        </FBI>
    </FileCheckerConfigGroup>
</configuration>

Code:

// Get the application configuration file. 
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

// Get the collection of the section groups. 
ConfigurationSectionGroupCollection sectionGroups = config.SectionGroups;

foreach (ConfigurationSectionGroup sectionGroup in sectionGroups)
{
    if (sectionGroup.Name == "FileCheckerConfigGroup")
    {
        foreach (ConfigurationSection configurationSection in sectionGroup.Sections)
        {
          var section = ConfigurationManager.GetSection(configurationSection.SectionInformation.SectionName) as NameValueCollection;
          inputDirectory = section["inputDirectory"]; //"C:\\testfiles";
        }
    }
}
Andy
  • 2,248
  • 7
  • 34
  • 57
  • How will you know which company's data to use? That is how do you know when you're a user at FBI? – DOK Jan 12 '11 at 15:45
  • after setting the input directory, there will be a method to do things for that company. – Andy Jan 13 '11 at 12:05

2 Answers2

89
<configSections>
  <section name="FBI" type="System.Configuration.NameValueSectionHandler" />
  <section name="FSCS" type="System.Configuration.NameValueSectionHandler" />
</configSections>

<FSCS>
  <add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>
</FSCS>
<FBI>
  <add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>
</FBI>

And then:

var section = ConfigurationManager.GetSection("FSCS") as NameValueCollection;
var value = section["processingDirectory"];
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    this is great. would you happen to know a way to retrieve all diffrent sections from the app config from the code? – Andy Jan 12 '11 at 16:03
  • 14
    Note: If you're using sectionGroups, the group gets added to the get sections as a path ie ConfigurationManager.GetSection("GroupName/FSCS") as NameValueCollection; – Andy Aug 15 '11 at 10:23
  • in .net version higher than 2.0 you have to use `type="System.Configuration.AppSettingsSection"` – rock_walker Jan 16 '18 at 14:02
17

App.config

<configSections>
  <sectionGroup name="FileCheckers">
    <section name="FBI" type="System.Configuration.NameValueSectionHandler" />
    <section name="FSCS" type="System.Configuration.NameValueSectionHandler" />
  </sectionGroup>
</configSections>

<FileCheckers>
  <FSCS>
    <add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>
  </FSCS>
  <FBI>
    <add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>
  </FBI>
</FileCheckers>

Example Usage

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSectionGroup fileCheckersGroup = config.SectionGroups["FileCheckers"];
foreach (ConfigurationSection section in fileCheckersGroup.Sections)
{
    NameValueCollection sectionSettings = ConfigurationManager.GetSection(section.SectionInformation.SectionName) as NameValueCollection;
    var value = sectionSettings["processingDirectory"]
}
Ryan Wheale
  • 26,022
  • 8
  • 76
  • 96
user1387916
  • 219
  • 2
  • 6
  • 1
    Please provide some explanation in addition to the code. – Matthijs Wessels Sep 19 '14 at 11:21
  • 1
    +1 for not using only "var" definitions. It helps understanding how it works or what kind of types you are using. – EAmez Mar 28 '17 at 14:50
  • For future web searchers, I created a custom ConfigSectionGroup by subclassing -- this seems wrong, as this code attempted to load the class from the .NET Configuration namespace, which resulting in a TypeLoadException. After further inspection of the code, it seems unnecessary. – ryanwebjackson Sep 06 '18 at 17:03
  • If you get a TypeInitializationException, ensure that the configSections element is the first child of the configuration element. There should also only be 1 of them. – FunksMaName Jan 16 '19 at 16:56