Reloading .NET config file
.net, c#, configuration, configuration-files
Solution
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);
}
}
Problem
I need to reload the configuration file after modifying it. How this can be done using appdomains? A code sample would be useful.