Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

app.config are not saving the values

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!

like image 614
Jack Avatar asked Jan 12 '12 19:01

Jack


1 Answers

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);
like image 66
RBDev Avatar answered Oct 10 '22 16:10

RBDev