Saving changes to an external config file specified in configSource attribute of custom section
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);
精彩评论