Jak używać sekcji w aplikacji c # 4.0.config?

Chcę użyć mojej aplikacji config do przechowywania ustawień dla 2 companys, i wolałbym, jeśli było możliwe, aby użyć sekcji Do oddzielenia danych dla jednego z drugim, a następnie podając im różne nazwy kluczy.

Sprawdzałem online, ale wydaje mi się, że jestem nieco przytłoczony, gdy ludzie używają sekcji lub znajdują przestarzałe łatwe sposoby ich użycia. czy ktoś mógłby mi podać poradnik dla początkujących?

Poniżej znajduje się przykład tego, co moja aplikacja.config wyglądałby 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>

Aktualizacja:

Zaawansowane rozwiązanie oparte na anwer. gdyby ktoś chciał wiedzieć.

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>

Kod:

// 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";
        }
    }
}
Author: F. Vrenegor, 2011-01-12

2 answers

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

A następnie:

var section = ConfigurationManager.GetSection("FSCS") as NameValueCollection;
var value = section["processingDirectory"];
 75
Author: Darin Dimitrov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-12 15:44:31

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>

Przykładowe Użycie

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"]
}
 10
Author: user1387916,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-17 19:05:18