gpt4 book ai didi

.net - 通过合并两个配置文件来创建 System.Configuration.Configuration?

转载 作者:行者123 更新时间:2023-12-03 17:58:51 76 4
gpt4 key购买 nike

我有两个 .config 文件,我需要一个 System.Configuration.Configuration那是它们之间的部分合并?

我可以将文件读取为 XML 并轻松创建所需的合并,但它是一个字符串或 XDocument。但是,.net 的 System.Configuration 似乎是严格基于文件的,所以我必须将它写入我想避免的临时目录。

有没有办法做到这一点?

例如,config1.config :

<configuration>
<appSettings>
<add key="CacheTime" value="300" />
</appSettings>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="MyBehavior">
<!-- snipped -->
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>

config2.config :
<configuration>
<system.serviceModel>
<client>
<endpoint name="MyEndpoint" address="net.tcp://...."
behaviorConfiguration="MyBehavior" binding="netTcpBinding"
contract="IMyContract">
</endpoint>
</client>
</system.serviceModel>
</configuration>

生成的配置应该是这两者的联合,这样我就有了 AppSetting 和 Endpoint。现在,此示例包含 WCF,但我不是在寻找特定于 WCF 的解决方案,因为我绝对需要一个 System.Configuration.Configuration 对象。

config1 和 config2 只是示例 - 它们的实际组合是不确定的,我可能有 config1 和 config4 或 config 3 和 config4 或 config2 和 config3。

最佳答案

如果您的目标只是让您的配置文件更加模块化(并可能保护对特定部分的文件访问),请查看 System.Configuration.SectionInformation.ConfigSource .

关于.net - 通过合并两个配置文件来创建 System.Configuration.Configuration?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6510927/

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