2009-10-13 30 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

相关问题