我可以使用ConfigurationManager修改自定义配置部分吗?

问题描述:

我有以下形式的App.config我可以使用ConfigurationManager修改自定义配置部分吗?

<?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> 

随着我继承了这个代码的形式

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

的自定义配置文件Custom.config,并没有与此相关的任何现有ConfigurationSectionConfigurationSectionHandlerCustom.Config部分。

我可以访问的foo就好值与

DirectCast(System.Configuration.ConfigurationManager.GetSection("Custom.Config"), System.Collections.IDictionary) 

但现在我想更新这个值,编程(用于测试目的)。 这可能吗?

我读过以下内容,我仍然难倒;他们似乎在暗示这个命名空间仅用于读取值,而不是完整的CRUD:

+0

无法更新和保存源中的值。根据您的部署设置的确切程度,可以在该阶段覆盖这些值。像Octopus这样的工具使用变换来完成。 – nurdyguy

你必须使用XDocument那么您修改磁盘上的文件Custom.config将需要拨打RefreshSectionConfigurationManager

然后这将刷新命名的部分,所以下次它被检索它将被重新读取从磁盘。

+0

震惊,这是System.Configuration命名空间内没有烘焙功能,但这是我自己解决了暂时。 – Matt

+0

在编写功能/验收测试之前,我手卷同样的东西。 –