是否可以使用 配置管理器 修改自定义配置节?



我有一个表单App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="Custom.Config" type="System.Configuration.DictionarySectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<kCura.Config configSource="Custom.config" />
</configuration>

使用表单Custom.config的自定义配置文件

<?xml version="1.0" encoding="utf-8"?>
<Custom.Config>
<add key="foo" value="bar" />
</Custom.Config>

我继承了这段代码,并且没有与此Custom.Config部分关联的现有ConfigurationSectionConfigurationSectionHandler

我可以使用foo

就可以访问的价值
DirectCast(System.Configuration.ConfigurationManager.GetSection("Custom.Config"), System.Collections.IDictionary)

但是现在我想以编程方式更新此值(用于测试目的(。这可能吗?

我已经阅读了以下内容,但我仍然感到困惑; 它们似乎暗示此命名空间仅用于读取值,而不是完整的 CRUD:

  • 如何:使用配置节创建自定义配置节
  • 如何:使用 IConfigurationSectionHandler 创建自定义配置节

您必须使用 XDocument 修改磁盘上的Custom.config文件,然后您需要在 ConfigurationManager 上调用RefreshSection

然后,这将刷新命名节,以便下次检索它时,将从磁盘中重新读取它。

最新更新