Skip to content

Instantly share code, notes, and snippets.

@daveaglick
Created August 22, 2017 11:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daveaglick/3eb907949c5058656c7f1f4d0fcbca62 to your computer and use it in GitHub Desktop.
Save daveaglick/3eb907949c5058656c7f1f4d0fcbca62 to your computer and use it in GitHub Desktop.
Change config file at runtime
// Set app.config - http://stackoverflow.com/a/6151688/807064
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", @"C:\new.config");
typeof(ConfigurationManager)
.GetField("s_initState", BindingFlags.NonPublic | BindingFlags.Static)
.SetValue(null, 0);
typeof(ConfigurationManager)
.GetField("s_configSystem", BindingFlags.NonPublic | BindingFlags.Static)
.SetValue(null, null);
typeof(ConfigurationManager).Assembly.GetTypes()
.Where(x => x.FullName == "System.Configuration.ClientConfigPaths")
.First().GetField("s_current", BindingFlags.NonPublic | BindingFlags.Static)
.SetValue(null, null);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment