0

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.

Kira
  • 1,153
  • 4
  • 28
  • 63
  • Possible duplicate of http://stackoverflow.com/questions/5274829/configurationmanager-appsettings-how-to-modify-and-save – Aaron Hawkins Nov 19 '14 at 21:32

2 Answers2

1

You need to save your changes

config.Save(ConfigurationSaveMode.Modified);

Also, if you want to reload the app.config

ConfigurationManager.RefreshSection("appSettings");
cost
  • 4,420
  • 8
  • 48
  • 80
1
System.Configuration.Configuration config = 
         ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings["DaysLeft"].Value = Days.Text.ToString();
config.Save(); 
T McKeown
  • 12,971
  • 1
  • 25
  • 32