IT TIP

C # 4.0 app.config에서 섹션을 어떻게 사용합니까?

itqueen 2020. 12. 30. 19:56
반응형

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>

최신 정보:

답변을 기반으로 한 고급 솔루션. 누군가가 알고 싶어하는 경우.

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

<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"];

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

참조 URL : https://stackoverflow.com/questions/4670669/how-do-you-use-sections-in-c-sharp-4-0-app-config

반응형