Dynamically modify the default app.config
In order to dynamically loading the module that needs to be configured, a method is required to create a new app.config in memory, integrate each section of the module, and allow applications to use it without covering the default app.config Essence
In response to this problem, we found a solution similar to the problem raised in the problem. However, the code provided does not produce the expected results. The method mentioned in the link problem can only be done before the configuration system is used for the first time. The cache value in the ClientConfigPaths class prevents reading new configuration after initial use.
Therefore, this solution has also deleted these cache values. The following code implements this task:
Using this code, the new app.config will be read and used when the application is running. You can restore the default app.config by release the AppConfig instance. If you need to change permanently, you can execute it without using the AppConfig.change method and place it at the beginning of the application.
<code class="language-csharp">public abstract class AppConfig : IDisposable { // ... } private class ChangeAppConfig : AppConfig { // ... private static void ResetConfigMechanism() { // ... typeof(ConfigurationManager) .Assembly.GetTypes() .Where(x => x.FullName == "System.Configuration.ClientConfigPaths") .First() .GetField("s_current", BindingFlags.NonPublic | BindingFlags.Static) .SetValue(null, null); } }</code>
The above is the detailed content of How Can I Dynamically Change the app.config at Runtime Without Overwriting the Default?. For more information, please follow other related articles on the PHP Chinese website!