6

ConfigurationManager.AppSettings 수 섹션 바로 아래에 있습니다. 그러나 나는 어떤 작품이나 더 나은 제안에도 만족할 것이다. 나는 지금이 내 소원을 하향 조정했습니다의 app.config : 나는 어떻게해야합니까 중첩 customSection라는 appSettings는 내 희망의 app.config는 다음과 같이 될 것이다

<configSections> 
    <sectionGroup name="QA_Environment"> 
     <section name="appSettings" type="System.Configuration.NameValueSectionHandler"/> 
    </sectionGroup> 
    <sectionGroup name="Production_Environment"> 
     <section name="appSettings" type="System.Configuration.NameValueSectionHandler"/> 
    </sectionGroup> 
</configSections> 

그리고 나는 내가 루트 수준 appSettings는 이러한 섹션 중 하나를 대체 할 수 있는지에 대해 궁금하네요 중요한 것은이 ... 좋은 것 같아요 ... 프로그래밍 방식으로 설정을 추가하거나 작성하여 저장하지 않고도 사용할 수 있습니다. 사용자가 환경을 선택할 수있게하려면 select 이벤트가 appSettings를 변경합니다.

내가 직면 한 하나의 제약은 내가 참조하는 데이터 레이어가 그대로 유지되어야한다는 것입니다. 그래서 .... 기본적으로 내 app.config를 액세스 할 수 있도록 설정해야합니다. 현재 ConfigurationManager.AppSettings입니다. [afdasdf]

명확한 설명이 필요하면 알려주세요. ... thanks

답변

4

괜찮 으면 나 자신의 질문에 대답하겠습니다. 나는 그것이 실제로 그것보다 훨씬 더 어렵게 만들고 있음을 발견했다.

<?xml version="1.0" encoding="utf-8"?> 

<configSections> 
    <sectionGroup name="Environment"> 
     <sectionGroup name="QA"> 
      <section name="databases" type="System.Configuration.DictionarySectionHandler"/> 
      <section name="storageSystems" type="System.Configuration.DictionarySectionHandler"/> 
     </sectionGroup> 
     <sectionGroup name="PROD"> 
      <section name="databases" type="System.Configuration.DictionarySectionHandler"/> 
      <section name="storageSystems" type="System.Configuration.DictionarySectionHandler"/> 
     </sectionGroup> 
    </sectionGroup> 
</configSections> 

<Environment> 
    <QA> 
     <databases> 
     </databases> 
     <storageSystems> 
     </storageSystems> 
    </QA> 

    <PROD> 
     <databases> 
     </databases> 
     <storageSystems> 
     </storageSystems> 
    </PROD> 
</Environment> 

그래서 제의 app.config의 한 부분이있다 .... 나머지는 그냥 간단합니다 : 당신이 할 수있는 일은 이것이다

private void GetConfigurationSettings(TargetEnvironments targetEnvironment) 
    { 
     var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
     var databases = new Hashtable(); 
     var storageSystems = new Hashtable(); 

     switch (targetEnvironment) 
     { 
      case TargetEnvironments.QA: 
       databases = (Hashtable)ConfigurationManager.GetSection("Environment/QA/databases"); 
       storageSystems = (Hashtable)ConfigurationManager.GetSection("Environment/QA/storageSystems"); 
       break; 
      case TargetEnvironments.PROD: 
       databases = (Hashtable)ConfigurationManager.GetSection("Environment/PROD/databases"); 
       storageSystems = (Hashtable)ConfigurationManager.GetSection("Environment/PROD/storageSystems"); 
       break; 
     } 

     foreach (string key in databases.Keys) { config.AppSettings.Settings.Add(key, databases[key].ToString()); } 
     foreach (string key in storageSystems.Keys) { config.AppSettings.Settings.Add(key, storageSystems[key].ToString()); } 

     config.Save(ConfigurationSaveMode.Modified); 

     ConfigurationManager.RefreshSection("appSettings"); 

     UpdateCollections(); 
    } 

참고 분명히 방금 설정 한 설정을 즉시로드하려면 config.Save 메서드를 사용하는 것이 중요합니다. 그 외에도 실제로 경로 이름과 섹션 유형을 결정해야합니다. 아래 링크가 가장 유용하다고 생각했습니다. 누군가가 더 우아한 방법을 가지고 있다면 그것에 대해 듣고 싶어 할 것입니다.

Here's the place I got the most out of in my research

2

배포 관련 web.config 파일을 처리하는 다른 방법이 있습니다. 편집 명령을 설명하는 배포 관련 web.config 파일을 기본 web.config 파일에 정의 할 수 있습니다 (모든 것을 반복하는 것이 아니라). this SO question에 대한 답변을 살펴보십시오.

기본적으로 프로젝트를 배포 할 때 기본 web.config 파일과 병합되는 web.debug.config 및 web.release.config 파일을 정의 할 수 있습니다.