I need to reload the configuration file after modifying it. How this can be done using appdomains? A code sample would be useful.
To reload Config, select the loaded configurations you want to reload and click Reload. You can only reload a configuration that has the status of Loaded. To refresh Configs, click Refresh. Information for all of the configs in the table is redisplayed.
There is a global configuration file for all sites in a given machine which is called Machine. config. This file is typically found in the C:\WINDOWS\Microsoft.NET\Framework\v2. 0.50727\CONFIG directory.
config file name extension is protected from being downloaded by ASP.NET. Click Add to create the file and open it for editing. The file contains the code shown in the "Example" section later in this topic, with a few initial defaults. Your application inherits all configuration settings from the Machine.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With