C# 如何在 .NET 中为自定义配置部分启用 configSource 属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/398607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to enable configSource attribute for Custom Configuration Section in .NET?
提问by E Rolnicki
following the wealth of information found herehow can we get an external .config to work? I've tried the same setup I would use for an external appSettings file, but it is unable to find the file for my custom section.
遵循此处找到的大量信息,我们如何才能使外部 .config 工作?我已经尝试了与用于外部 appSettings 文件的设置相同的设置,但无法为我的自定义部分找到该文件。
<configSections>
...
<section name="CustomSettings" type="Fully.Qualified.TypeName.CustomSettings, AssemblyName" />
</configSections>
<!-- this works -->
<CustomSettings attrib1="val1" attrib2="val2" .../>
however...
然而...
<!--this does not work-->
<CustomSettings configSource="someExternalFile.config"/>
where someExternalFile.config would contain
其中 someExternalFile.config 将包含
<CustomSettings attrib1="val1" attrib2="val2" .../>
any ideas?
有任何想法吗?
采纳答案by Charles Bretana
The actual file, must be placed relative to the project output folder (by default "\bin\debug" or "bin\Release"
实际文件,必须相对于项目输出文件夹放置(默认情况下为“\bin\debug”或“bin\Release”
Also, The file in your project tree, look at the properties of the file, and make sure the "Copy to Output Directory" setting is set to "Copy Always" or "Copy if Newer"
此外,项目树中的文件,查看文件的属性,并确保“复制到输出目录”设置设置为“始终复制”或“如果较新则复制”
EDIT: make sure the separate config file has an xml Element header. The entire file contents should read as follows:
编辑:确保单独的配置文件有一个 xml 元素标头。整个文件内容应如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<CustomSettings attrib1="val1" attrib2="val2" .../>