代码之家  ›  专栏  ›  技术社区  ›  Alexis Abril

将自定义配置组移动到单独的文件

  •  18
  • Alexis Abril  · 技术社区  · 15 年前

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

    这与appSettings的file属性类似。我意识到很可能需要为我的自定义节处理程序创建ConfigurationPropertyAttribute,但是我在这方面没有找到任何示例或方向。

    1 回复  |  直到 15 年前
        1
  •  35
  •   marc_s    10 年前

    据我所知,你 不能 将整个分区组外部化(即。 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>