1

I've got an exe that reads some values from its local app.config file:

TargetDate = ConfigurationManager.AppSettings.Get("ThresholdDate");

// and try to update with the current date
ConfigurationManager.AppSettings.Set("ThresholdDate", "2011-09-01");

I thought it worked once, but am not seeing the app.config getting updated at all now.

Yuck
  • 49,664
  • 13
  • 105
  • 135
user360943
  • 109
  • 2
  • 9
  • 1
    is this helpful: http://stackoverflow.com/questions/980440/update-app-config-system-net-setting-at-runtime – Baz1nga Sep 01 '11 at 17:43

3 Answers3

2

I think you can try something like this:

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
//change the config value
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

I am not sure abt the syntax for changing the config value but I have done an Add before and I know you can do a remove so I guess you could do a combination of remove and add like this:

config.AppSettings.Settings.Remove("ThresholdDate");
config.AppSettings.Settings.Add("ThresholdDate", "2011-09-01");
Baz1nga
  • 15,485
  • 3
  • 35
  • 61
2

Looking here: How to change App.config file run time using C# is the answer - while running in the visual studio IDE, you will not see the values persisted in the /bin/appname.exe.config. You actually have to go into the bin directory and run the exe.

So this code actually works, just not in debug mode:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

config.AppSettings.Settings["ThresholdDate"].Value = Convert.ToString(testdate);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
Matthieu
  • 4,605
  • 4
  • 40
  • 60
user360943
  • 109
  • 2
  • 9
  • 2
    if it solved your issue, you should mark this as your accepted answer (48hrs timer should be expired now) – Matthieu Sep 27 '11 at 17:45
-1
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

config.AppSettings.Settings["ThresholdDate"].Value = Convert.ToString(testdate);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

this will work only while your not in debuging mode. try to run yung code in /debug/appName.exe and the you will se the changes in appName.exe.config

cheers!

jersoft
  • 478
  • 2
  • 9
  • 20