开发者

Saving changes to an external config file specified in configSource attribute of custom section

开发者 https://www.devze.com 2023-03-05 07:47 出处:网络
I have defined a custom section in the App.config file and all the Configuration properties present for the customSection have been defined in an external config file specified in configSource file.

I have defined a custom section in the App.config file and all the Configuration properties present for the customSection have been defined in an external config file specified in configSource file.

Now, the scenarios is that whenever I run the program I modify the values of the properties present in the external config file and I need a way to save these values in the external config file.

How would it be possible to save these values? Is using the normal ways to write to a file the only option?

Consider the following scenario as to wh开发者_StackOverflow中文版at I want to do in the application:

App.config file

<?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."/>

Suppose the value of version is changed to "Foo" during program execution. How can I save that value in example.config file permanently so that when I exit the application and reload it again, the value of version will be Foo.


You can do it like this. Configuration c = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); c.AppSettings.Settings["Your config"].Value=....; c.Save(ConfigurationSaveMode.Modified);

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号