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"); } |