2011-01-12 11 views
51

私は2つの会社の設定を保存するために私のアプリの設定を使用したいと思います。別のキー名を与えるのではなく、別のセクションのデータを分離するためにセクションを使用することができたら、c#4.0 app.configのセクションはどのように使用しますか?

私はオンラインでチェックしていますが、人々がセクションを使用したり、それらを使用する古い方法を見つけるときに圧倒されるようです。誰も私にそれらの初心者ガイドを渡すことができますか?

以下は私のApp.configファイルがどのように見えるかの例です:

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

更新:anwerに基づいて

高度なソリューションを提供します。誰でも知りたいと思った場合に備えて。

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> 

コード:

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

はどのように使用することが、企業のデータを知っているのだろうか?それはあなたがFBIのユーザーであることをどのように知っていますか? – DOK

+0

入力ディレクトリを設定した後、その会社のための方法があります。 – Andy

答えて

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> 

そして:

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

これは素晴らしいです。あなたはコードからアプリの設定からすべての異なるセクションを取得する方法を知っているだろうか? – Andy

+10

注:sectionGroupsを使用している場合、グループはパスとしてgetセクションに追加されます。つまり、NameValueCollectionとしてConfigurationManager.GetSection( "GroupName/FSCS")が追加されます。 – Andy

+0

の.netバージョンが2.0より高い場合は、 '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> 

使用例

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

を使用する必要があります。コードに加えていくつかの説明を加えてください。 –

+0

"var"定義のみを使用しない場合は+1。どのように動作するか、使用している種類の種類を理解するのに役立ちます。 – EAmez

関連する問題