HowTo update appsettings programmatically

Recently I was writing functional test for an application that read settings from the appsettings element in App.Config. The problem was that the application under test was expecting to react different based on the settings so need a way to alter the values between tests. I ended up writing bellow code for it

private static void SetAppSettings(string key, string value)
{
   Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
   config.AppSettings.Settings[key].Value = value;
   config.Save(ConfigurationSaveMode.Modified);
   ConfigurationManager.RefreshSection("appSettings");
}
0.00 avg. rating (0% score) - 0 votes

About Peter Wibeck

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!

*