2011-05-13 230 views
1

我在App.config文件定义的自定义部分和所有存在的customSection配置属性已在configSource文件中指定的外部配置文件中定义的定制部分的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。

回答

0

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

+0

我会尝试了这一点,并让你知道。但这是自定义部分,我没有将它存储在应用程序设置 – chaitanya 2011-05-13 05:48:30

+0

您可以阅读[http://msdn.microsoft.com/en-us/library/ms178684(v=VS.90).aspx](http:/ /msdn.microsoft.com/en-us/library/ms178684(v=VS.90).aspx)。 – Justin 2011-05-13 06:32:48