My App.Config
is something like:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="foo" value=""/>
</appSettings>
</configuration>
I try to save the foo
value using the following method:
private void SaveValue(string value) {
var config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Add("foo", value);
config.Save(ConfigurationSaveMode.Modified);
}
but this not change the value of it. and I don't get a exception. how to fix this? thanks in advance!
When you are debugging with Visual Studio probably the <yourexe>.vshost.exe.config
is modified instead of the <yourexe>.exe.config
. When you build the application in Release mode only the <yourexe>.exe.config
exists and will be updated.
Your code will also add an extra node to the configuration file. Use something like the code below to update the setting:
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["foo"].Value = "text";
config.Save(ConfigurationSaveMode.Modified);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With