I need to reload the configuration file after modifying it. How this can be done using appdomains? A code sample would be useful.
-
Do we really need to reload the config file after making some changes to it !?, I don't know, I am just asking, because I remember reading some where that we don't need to reload. Thanks! – Mahesh Velaga Mar 17 '10 at 19:43
-
What kind of program is this? Windows Service? ASP.NET? – John Saunders Mar 17 '10 at 20:01
-
possible duplicate of [Reload configuration settings from an external config file during run-time](http://stackoverflow.com/questions/4934898/reload-configuration-settings-from-an-external-config-file-during-run-time) – farid bekran Jun 24 '15 at 09:26
5 Answers
Let's say you have the following config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="test" value="1" />
</appSettings>
</configuration>
Let's try the naive approach first. The following application will try to grab the value of the appSetting
named test
once per second, and print its value:
static void Main(string[] args)
{
while(true)
{
Console.WriteLine(ConfigurationManager.AppSettings["test"]);
Thread.Sleep(1000);
}
}
But alas! While this is running, you'll notice it keeps printing 1
, and doesn't pick up any changes.
If you update your code to the following, it will fix this issue, and it will pick up changes whenever you change it:
static void Main(string[] args)
{
while(true)
{
ConfigurationManager.RefreshSection("appSettings");
Console.WriteLine(ConfigurationManager.AppSettings["test"]);
Thread.Sleep(1000);
}
}

- 17,332
- 11
- 62
- 86
-
1Pity this post wasn't marked as answer. I tend to prefer more _verbose_ answers, but well, its just me. – Eric Wu Aug 23 '16 at 14:45
ConfigurationManager.RefreshSection("configuration");
Properties.Settings.Default.Reload();

- 415
- 1
- 7
- 29
Yes, it's possible... depending on HOW you access your configuration file.
If you rely on the default behavior, then the answer is NO.
However, if you access the configuration through a static
property of method common to your project, then it's possible to reload it.
I don't have the code snippet with me now, but I did something similar even using a FileSystemWatcher
to detect changes in the config
file.
There's one caveat, it works only with properties that you access directly through your code, automatic configuration will not be reloaded when you do such thing.

- 11,285
- 4
- 39
- 65
I have actually found the solution to the given problem. Below are fiew lines of code of how this can be done:
AppDomainSetup domaininfo = new AppDomainSetup();
domaininfo.ApplicationBase = System.Environment.CurrentDirectory;
domaininfo.ConfigurationFile = "Target_Config.exe.config";
Evidence adevidence = AppDomain.CurrentDomain.Evidence;
AppDomain dom = AppDomain.CreateDomain("test", adevidence, domaininfo);
var someType =(SomeType)dom.CreateInstanceAndUnwrap("Target_Assembly",
"Target_Assembly.SomeType");
The key point here is the AppDomainSetup class, which allows to set the configurationfile property on the assembly to be created. Now, we can monitor the configuration file "Target_Config.exe.config" for changes. When it is changed, the above created appdomain should be unloaded and then recreated.

- 3,547
- 10
- 39
- 55
-
This is probably a solution to reusing a configuration file across several applications without copying it for each application. – Markus Mar 17 '10 at 20:28