c# - 如何在 .NET 中为自定义配置部分启用 configSource 属性?

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

下面找到了丰富的资料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" .../>

有什么想法吗?

最佳答案

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

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

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

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

关于c# - 如何在 .NET 中为自定义配置部分启用 configSource 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/398607/

相关文章:

c# - 您如何维护多个版本的数据库?

c# - POCO/Domain 对象是否可以注入(inject)依赖项

spring - 在 Spring 中添加新属性源的最佳方法是什么?

database - 您如何在数据库中构造配置数据?

c# - wpf richtextbox 中没有上标

c# - 将 TelemetryClient 的静态实例与 Application Insights 结合使用

c# - .NET 无状态多重 PermitIf

c# - 为什么 ImmutableArray.Create 复制现有的不可变数组?

php - MySQL 的最佳配置? InnoDB 和 MyISAM

c# - 使用存储过程时,不能多次枚举查询结果