Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is switching app.config at runtime possible?

Is there a way at runtime to switch out an applications app.config (current.config to new.config, file for file). I have a backup/restore process which needs to replace its own application.exe.config file. I have seen this post but it does not answer how to do this at runtime.

like image 940
maxfridbe Avatar asked Jan 12 '09 19:01

maxfridbe


People also ask

Can we change the configuration file at run time?

You can change and save these using the ConfigurationManager . You can modify and save user settings without restarting. You can't do that with application settings.

Can we have multiple app config files?

You cannot use multiple configuration files (i.e. one per library project) without coding. Option: You can use the ConfigurationManager Class to load an alternate config file by code.

When should I use app config?

App. Config is an XML file that is used as a configuration file for your application. In other words, you store inside it any setting that you may want to change without having to change code (and recompiling). It is often used to store connection strings.


2 Answers

Turns out I can swap the .config file for the new one and do a ConfigurationManager.RefreshSection(...) for each section. It will update from the new .config file.

like image 89
maxfridbe Avatar answered Sep 22 '22 05:09

maxfridbe


Microsoft .NET's app.config is not designed for your scenario, as well as many others. I often encounter a similar need, so I have spent a lot of effort designing a solution.

  1. Redesign to use app.config only as a configuration bootstrap: specify where to find the rest of the real configuration data. This information should almost never change, so there is no need to handle file watching or application restarts.

  2. Pick an alternate location for the real configuration data: a file, a database, perhaps even a web service. I prefer a database most of the time, so I create a configuration table with a simple structure that allows me to store my data.

  3. Implement a simple library to wrap your configuration access so that you have a simple API for the rest of your application (via dependency injection). Hide the usage of app.config as well as your real configuration storage location(s). Since .NET is strongly-typed, make the configuration settings so--convert each string retrieved into the most-specific type available (URL, Int32, FileInfo, etc.).

  4. Determine which configuration settings can be safely changed at runtime versus those that can't. Typically, some settings need to change along with others, or it simply makes no sense to allow them to change at all. If all your configuration data can safely change at runtime, then that makes things easy, but I HIGHLY doubt such a scenario. Hide the changeability and interdependencies of the configuration settings to the extent possible.

  5. Design the response to the unavailability of your real configuration data. I prefer to treat the absence of any configuration setting as a fatal error that aborts the application, unless I can identify a usable default. Likewise, I abort in the absence of the configuration storage container (file, database table, etc.).

Enjoy, and best wishes.

like image 44
Rob Williams Avatar answered Sep 24 '22 05:09

Rob Williams