2009-10-13 62 views
18

我最近寫了一個相當大的自定義配置組。我很好奇,如果有可能通過以下這個配置移動到一個單獨的文件:移動定製配置組到一個單獨的文件

<configuration> 
    <configSections> 
     <sectionGroup name="MyCustomGroup"> 
      <section name="MyCustomSection"/> 
     </sectionGroup> 
    </configSections> 
    <MyCustomGroup file="alt.config" /> 
</configuration> 

這類似於你可以與的appSettings文件屬性做什麼東西。我意識到最有可能需要爲我的自定義部分處理程序創建一個ConfigurationPropertyAttribute,但是在這方面我找不到任何示例或方向。

回答

27

據我所知,你無法外部化整個SectionGroup(即MyCustomGroup)使用configSource屬性,但你必須處理這個對科級(即MyCustomSection

<configuration> 
    <configSections> 
     <sectionGroup name="MyCustomGroup"> 
       <section name="MyCustomSection"/> 
     </sectionGroup> 
    </configSections> 
    <MyCustomGroup>  
     <MyCustomSection configSource="externalfile.config" /> 
    </MyCustomGroup> 
</configuration> 

外部那麼文件externalfile.config將包含您的實際的配置設置,直接與自己的自定義標籤節開始(沒有前導<?xml....?><configuration>或任何需要):

<MyCustomSection> 
    ... your settings here...... 
</MyCustomSection> 

馬克

+1

就像一個魅力。 – 2009-10-13 20:48:34

+1

你是對的。章節組不能整體外化,但章節可以。 – 2013-12-12 10:33:20

+0

@marc_s - 太棒了,我不知道這是一個如此古老的問題。我只是google搜索,並發現這是最重要的結果! – Liath 2014-11-06 14:27:48

相關問題