从app.config中的某个部分动态添加和删除键值对

时间:2014-12-11 19:39:47

标签: c# xml configuration app-config

我的XML文件如下:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="databaseTypes" type="System.Configuration.NameValueSectionHandler" />
    <section name="dataDictionary" type="System.Configuration.NameValueSectionHandler" />
  </configSections>
  <databaseTypes>
    <add key="ExampleServerPrefix_T" value="Connection_String_For_ExampleServer" />
    <add key="ExampleServer2Prefix_T" value="Connection_String_For_ExampleServer_2" />
    <add key="COPYLIVE_" value="ODBC;DSN=s2;" />
  </databaseTypes>
  <dataDictionary>
    <!-- Other pairs in this section -->
  </dataDictionary>
</configuration>

我想要实现的是能够从databaseTypes部分添加和删除键值对。例如,想要动态添加一对新运行时间<add key="blah" value="ODBC;blah" />

首先知道这有用吗?如果是这样的话,因为我找不到任何相关的文档或示例来说明这是如何完成的。

1 个答案:

答案 0 :(得分:1)

钟,

以下两篇文章将帮助您向配置中添加键/值对:

将自定义部分编写到app.config

Writing custom sections into app.config

在运行时更新App.config中的AppSettings和自定义配置部分

http://yizeng.me/2013/08/31/update-appsettings-and-custom-configuration-sections-in-appconfig-at-runtime/

相关问题