Can't modify app.config file with no error

196 views Asked by At

I'm struggling with trying to modify the appSettings part of the app.config file of my application. However I'm not receiving any error/exception and I can read values without any problem. Here is how I tried to set values :

ConfigurationManager.AppSettings.Set("DaysLeft", Days.Text.ToString());
//    
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["DaysLeft"].Value = Days.Text.ToString();
//when I debug the app, I notice that the value is changed in the previous line

The app.config file isn't being modified.

2

There are 2 answers

0
T McKeown On BEST ANSWER
System.Configuration.Configuration config = 
         ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["DaysLeft"].Value = Days.Text.ToString();
config.Save(); 
0
cost On

You need to save your changes

config.Save(ConfigurationSaveMode.Modified);

Also, if you want to reload the app.config

ConfigurationManager.RefreshSection("appSettings");