3

What's wrong with the following code:

private static void UpdateAppSettings(string settingName, string settingValue)
{
    if (settingName == null) throw new ArgumentNullException("settingName");
    if (settingValue == null) throw new ArgumentNullException("settingValue");

    var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    var appSettings = config.AppSettings;

    var setting = appSettings.Settings[settingName];
    setting.Value = settingValue;

    config.Save(ConfigurationSaveMode.Modified);
    ConfigurationManager.RefreshSection("AppSettings");
}

I don't know why it doesn't save the new settings. It first opens the app.config file. After going to appsetting area. It then finds a specific key as settingName and changes the value to settingValue. Afterwards, it saves the file and refreshes it. It almost works; up to config.Save(...). But after that I don't know what happens.

AstroCB
  • 12,337
  • 20
  • 57
  • 73
aakpro
  • 1,538
  • 2
  • 19
  • 53

1 Answers1

1

This question seems to be the same as what you are asking.

The answer looks to be this:

config.AppSettings[settingName] = settingValue;

EDIT:

This question has the answer I think

config.AppSettings.Settings[settingName]
Community
  • 1
  • 1
Carl Winder
  • 938
  • 8
  • 18