Cómo se utilizan las secciones en la aplicación c# 4.0.config?


Quiero usar la configuración de mi aplicación para almacenar la configuración de 2 compañías, y preferiría que fuera posible usar una sección para separar los datos de una de la otra en lugar de darles nombres de clave diferentes.

He estado revisando en línea, pero parece que me siento un poco abrumado cuando la gente usa secciones o encuentra formas fáciles de usarlas desactualizadas. ¿alguien podría pasarme una guía para principiantes?

A continuación se muestra un ejemplo de lo que mi aplicación.config se vería como:

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

Actualización:

Solución avanzada basada en el anwer. en caso de que alguien quisiera saberlo.

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>

Código:

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

Y luego:

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/ajaxhispano.com/template/agent.layouts/content.php on line 61
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>

Ejemplo de uso

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/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-03-17 19:05:18