我正在玩如何使用Configuration Manager在App.config文件中读取/写入C#中的
WPF应用程序的自定义部分.我在
.NET 2.0 Configuration Demystified阅读了这篇优秀文章,它帮助我使用配置文件.这是我写的初始App.config文件,它工作正常.
App.config中
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <configSections>
- <section name="example" type="CustomConfig.ExampleSection,CustomConfig" />
- </configSections>
- <example version="A sample string value." />
- <appSettings>
- <add key="version_string" value="1.01" />
- </appSettings>
- </configuration>
但是,当我更改App.config文件时,我的自定义部分将从configSource中提到的外部配置文件中读取,Visual Studio给出了一个错误
The format of a configSource file must be an element containing the name
of the section.
这是App.config和example.config文件
更改了App.config
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <configSections>
- <section name="example" type="CustomConfig.ExampleSection,CustomConfig" />
- </configSections>
- <example configSource="example.config" />
- <appSettings>
- <add key="version_string" value="1.01" />
- </appSettings>
- </configuration>
example.config
- <?xml version="1.0"?>
- <example>
- <add key="version" value="blahblah" />
- </example>
解决方法
Visual Studio的编辑器/ intellisense有一个缺点,它抱怨configSource =属性 – 但它绝对合法,它确实有效;我每天都在各种生产系统中使用它.
我的建议:试试吧! :-)运行代码 – 我很确定它会起作用(你的配置看起来不错).
更新:好的 – 似乎你完全改变了< example>的风格标签.在您原来的app.config中,您有:
- <example version="A sample string value." />
当然,您的外化example.config必须包含相同的值和相同的结构:
- <?xml version="1.0"?>
- <example version="A sample string value." />
你可以尝试这个example.config ??