0

I have a Windows Service written, and want to modify it's config file. I have an app in the same solution for this:

 private ExeConfigurationFileMap map = new ExeConfigurationFileMap();
 map.ExeConfigFilename = @"pathToMyWSConfigFile";

and then I do:

Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
config.AppSettings.Settings.Add("ServerPath", "test replace ");

config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");

However this simply adds another value to the ServerPath key in config file, and I want to replace it, so that there is always only one value there. How can I do it?

Januszoff
  • 315
  • 1
  • 16
  • possible duplicate of [ConfigurationManager.AppSettings - How to modify and save?](http://stackoverflow.com/questions/5274829/configurationmanager-appsettings-how-to-modify-and-save) – Ben Jul 23 '15 at 06:35
  • Take a look here: http://stackoverflow.com/questions/11149556/app-config-change-value – Tomaz Tekavec Jul 23 '15 at 06:36

1 Answers1

1

To update a setting, simply use the Settings indexer.

config.AppSettings.Settings["ServerPath"].Value = "test replace";
Bauss
  • 2,767
  • 24
  • 28