When getting the file path of the current configuration file, what is the difference in using the AppDomain
namespace and the ConfigurationManager
namespace? And when would you use one over the other?
For example, both return the same path:
string f1 = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
string f2 = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None ).FilePath;
It comes down to when and why you want a configuration file. The gist of it is this:
As a rough example, let's take a hypothetical application that can use plugins that it adds/removes on the fly. You don't want to have to keep those plugin assemblies in memory for the life of the application, that would defeat the purpose, so you create a separate AppDomain within your application. It will handle the loading and communication between the app and the plugin assemblies, do whatever you need to do with them and the app can unload the assemblies by dropping the AppDomain whenever it needs to.
The plugin AppDomain has quite a few settings that you'd rather keep separate from the client configuration file, so when you create the AppDomain you specify the separate file location. Within that AppDomain, the configuration file is that file.
The client configuration, though, may depend on who's using it (and they may have the ability to change it and customize their settings). You'd rather use an application-wide configuration that's segregated by a given user instead and not even give them the option to mess with the plugin-specific settings (or other user's settings). The ConfigurationManager could theoretically pull from any number of files.
That's a very general idea that handwaves away all of the implementation, but hopefully that starts to illustrates how the two might differ.
Here are the MSDN pages for both the AppDomainSetup and OpenExeConfiguration() as well, which may be useful and has additional links to configuration-related resources.
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