我有一个表单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
部分关联的现有ConfigurationSection
或ConfigurationSectionHandler
。
我可以使用foo
DirectCast(System.Configuration.ConfigurationManager.GetSection("Custom.Config"), System.Collections.IDictionary)
但是现在我想以编程方式更新此值(用于测试目的(。这可能吗?
我已经阅读了以下内容,但我仍然感到困惑; 它们似乎暗示此命名空间仅用于读取值,而不是完整的 CRUD:
- 如何:使用配置节创建自定义配置节
- 如何:使用 IConfigurationSectionHandler 创建自定义配置节
您必须使用 XDocument 修改磁盘上的Custom.config
文件,然后您需要在 ConfigurationManager 上调用RefreshSection
然后,这将刷新命名节,以便下次检索它时,将从磁盘中重新读取它。