gpt4 book ai didi

c# - 在 configSource 中使用外部 .config 文件会产生错误

转载 作者:太空狗 更新时间:2023-10-29 18:19:12 24 4
gpt4 key购买 nike

我正在研究如何使用配置管理器在 C# 中为 WPF 应用程序读取/写入 App.config 文件中的自定义部分。我在 .NET 2.0 Configuration Demystified 上阅读了这篇优秀的文章它对我使用配置文件有很大帮助。这是我编写的初始 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>

例子.config

<?xml version="1.0"?>
<example>
<add key="version" value="blahblah" />
</example>

最佳答案

我遇到了同样的错误。在我的例子中是因为我在两个文件中有 key ,然后检测到 appSettings 标签是重复的。

如果您需要在 web.config 中保存一些与项目相关的 key 并将您的个性化 key 保存在另一个文件中(出于安全原因推荐),请使用 file 属性而不是 configSource

web.config 文件:

<configuration>
<appSettings file="../AppSettings.config">
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
</appSettings>
</configuration>

AppSettings.config 文件:

<?xml version="1.0"?>

<appSettings>
<add key="RutaBodega" value="D:\Test\Card"/>
<add key="CodeLen" value="5"/>
</appSettings>

希望对其他人有帮助!

关于c# - 在 configSource 中使用外部 .config 文件会产生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5980667/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com