2011-01-12 3 views
51

2 개의 companys에 대한 설정을 저장하기 위해 앱 구성을 사용하고 싶습니다. 섹션을 사용하여 서로 다른 키를 제공하는 대신 다른 데이터를 구분할 수 있다면 더 좋을 것입니다.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

이것은 훌륭합니다. 코드에서 app config의 모든 다른 섹션을 검색하는 방법을 알고 계시겠습니까? – Andy

+10

참고 : sectionGroups를 사용하는 경우 그룹은 경로로 get 섹션에 추가됩니다. 즉, ConfigurationManager.GetSection ("GroupName/FSCS")을 NameValueCollection으로 추가합니다. – 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

관련 문제