【问题标题】:Saving changes to an external config file specified in configSource attribute of custom section将更改保存到自定义部分的 configSource 属性中指定的外部配置文件
【发布时间】:2011-08-24 13:53:17
【问题描述】:

我在 App.config 文件中定义了一个自定义部分,并且该 customSection 的所有配置属性都已在 configSource 文件中指定的外部配置文件中定义。

现在的情况是,每当我运行程序时,我都会修改外部配置文件中存在的属性值,我需要一种方法将这些值保存在外部配置文件中。

如何保存这些值?使用常规方式写入文件是唯一的选择吗?

考虑以下关于我想在应用程序中做什么的场景:

App.config 文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="example" type="CustomConfig.ExampleSection, 
                                 CustomConfig"  />
  </configSections>

  <example
   configSource="example.config"
  />

  <appSettings>
    <add key="version_string" value="1.01" />
  </appSettings>
</configuration>

example.config

<?xml version="1.0"?>
<example version="A sample string value."/>

假设在程序执行期间版本的值被更改为“Foo”。如何将该值永久保存在 example.config 文件中,以便当我退出应用程序并再次重新加载它时,版本的值将是 Foo。

【问题讨论】:

    标签: c# .net app-config


    【解决方案1】:

    你可以这样做。 Configuration c = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); c.AppSettings.Settings["Your config"].Value=....; c.Save(ConfigurationSaveMode.Modified);

    【讨论】:

    猜你喜欢
    • 2010-09-28
    • 1970-01-01
    • 2010-11-18
    • 1970-01-01
    • 2013-03-25
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    相关资源
    最近更新 更多