2011-01-12 2 views
51

Ich möchte meine App-Konfiguration verwenden, um die Einstellungen für 2 Unternehmen zu speichern, und ich würde es vorziehen, wenn es möglich wäre, einen Abschnitt zu verwenden, um die Daten voneinander zu trennen, anstatt ihnen verschiedene Schlüsselnamen.Wie verwenden Sie Abschnitte in C# 4.0 app.config?

Ich habe online überprüft, aber ich scheine ein wenig überwältigt zu werden, wenn Leute Abschnitte verwenden oder veraltete einfache Wege finden, sie zu benutzen. Könnte mir jemand einen Anfängerführer geben?

Unten ist ein Beispiel dafür, was meine app.config aussehen würde:

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

Erweiterte Lösung auf dem anwer basiert. falls jemand es wissen wollte.

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"; 
     } 
    } 
} 
+0

Wie werden Sie wissen, welche Unternehmen die Daten zu benutzen? Das ist, woher weißt du, wenn du ein Benutzer am FBI bist? – DOK

+0

nach dem Festlegen des Eingabeverzeichnisses wird es eine Methode geben, um Dinge für diese Firma zu tun. – Andy

Antwort

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

Und dann:

var section = ConfigurationManager.GetSection("FSCS") as NameValueCollection; 
var value = section["processingDirectory"]; 
+1

das ist großartig. Würdest du zufällig einen Weg finden, alle verschiedenen Abschnitte aus der App-Konfiguration aus dem Code zu holen? – Andy

+10

Hinweis: Wenn Sie sectionGroups verwenden, wird die Gruppe den get-Abschnitten als Pfad hinzugefügt, z. B. ConfigurationManager.GetSection ("GroupName/FSCS") als NameValueCollection; – Andy

+0

in .net-Version höher als 2.0 müssen Sie 'type =" System.Configuration.AppSettingsSection "' –

9

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> 

Beispiel Verwendung

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"] 
} 
+1

verwenden. Bitte erläutern Sie zusätzlich zum Code. –

+0

+1 für die Verwendung von "var" -Definitionen. Es hilft zu verstehen, wie es funktioniert oder welche Art von Typen Sie verwenden. – EAmez