【问题标题】:How to enable configSource attribute for Custom Configuration Section in .NET?如何在 .NET 中为自定义配置部分启用 configSource 属性?
【发布时间】:2010-09-28 18:08:24
【问题描述】:

根据here 找到的大量信息,我们如何才能让外部.config 工作?我尝试了用于外部 appSettings 文件的相同设置,但它无法找到我的自定义部分的文件。

<configSections>
...
    <section name="CustomSettings" type="Fully.Qualified.TypeName.CustomSettings, AssemblyName" />
</configSections>
<!-- this works -->
<CustomSettings attrib1="val1" attrib2="val2" .../>

不过……

<!--this does not work-->
<CustomSettings configSource="someExternalFile.config"/>

someExternalFile.config 将包含的位置

<CustomSettings attrib1="val1" attrib2="val2" .../>

有什么想法吗?

【问题讨论】:

    标签: c# .net configuration app-config configuration-files


    【解决方案1】:

    实际文件,必须相对于项目输出文件夹放置(默认为“\bin\debug”或“bin\Release”

    另外,项目树中的文件,查看文件的属性,并确保“复制到输出目录”设置设置为“始终复制”或“如果较新则复制”

    编辑:确保单独的配置文件具有 xml 元素标头。整个文件内容应如下所示:

    <?xml version="1.0" encoding="utf-8" ?>
    <CustomSettings attrib1="val1" attrib2="val2" .../>
    

    【讨论】:

    • 确保文件有一个 xml 标头元素,如我编辑的答案中所示...另外,您是否收到错误消息“无法找到外部文件...”
    猜你喜欢
    • 2011-08-24
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 2016-11-07
    相关资源
    最近更新 更多