2017-08-14 111 views
0

我有以下形式的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

回答

0

你必須使用XDocument那麼您修改磁盤上的文件Custom.config將需要撥打RefreshSectionConfigurationManager

然後這將刷新命名的部分,所以下次它被檢索它將被重新讀取從磁盤。

+0

震驚,這是System.Configuration命名空間內沒有烘焙功能,但這是我自己解決了暫時。 – Matt

+0

在編寫功能/驗收測試之前,我手卷同樣的東西。 –

相關問題