32
votes

OpenExeConfiguration has 2 overloads:

OpenMappedExeConfiguration has only 1 prototype:

It seems both (2) and (3) can be used to open a specific config file rather than the default app.config file.

So what's the difference between them? When to use which?

Why do we seperate the UserLevel and Config File Location in (1) and (2), but combine them in (3)?

Thanks for any replies.

Update

I know that Microsoft always like to do things in more than one ways. But it should do it for a reason. Any body know the reason in my question? Do we need a bounty ;) ?

2

2 Answers

41
votes

The difference is explained in the ultimate .NET config resource - Cracking the Mysteries of .NET 2.0 Configuration:

OpenExeConfiguration (String)

will append ".config" to the filename you provide and load that configuration file. It's important to note that OpenExeConfiguration(string exePath) is a very misleading method, as the filename does not have to be the filename of the .exe that is running [...] By providing a filename other than the EXE filename, an alternate *.config file can be opened.

OpenExeConfiguration (ConfigurationUserLevel)

The second method, OpenExeConfiguration(ConfigurationUserLevel level) will load the appropriate configuration file for the specified configuration level. Configuration levels, available in the Exe context, allow you to specify whether you want exe, roaming user, or local user configuration [...] Remember that configuration is hierarchical and merged. When requesting roaming or local user configuration, that level up through machine.config are merged, resulting in the complete configuration accessible by your application for the given user level.

OpenMappedExeConfiguration(), OpenMappedMachineConfiguration()

Unlike the OpenExeConfiguration() methods, which make several assumptions about where your configuration files reside, OpenMappedExeConfiguration() and OpenMappedMachineConfiguration() allow you to explicitly specify where your *.config files reside on disk. Using these methods, you can load an alternate machine.config, load User.config files from the locations of your own choosing (vs. letting the .NET framework decide on some convoluted path), etc. When accessing machine.config, a custom version is not required, OpenMachineConfiguration() should be used instead.

7
votes

The difference between OpenExeConfiguration (String) & OpenMappedExeConfiguration (ExeConfigurationFileMap, ConfigurationUserLevel) ) is that the mapped version will let you choose the configuration file that you wish to open using an ExeConfigurationFileMap.

If you use the OpenExeConfiguration(string) overload then it will open the configuration using the Machine and Exe config locations whereas the mapped version will let you pick the specific file you wish to load from any location (obviously still respecting permissions, etc.).

If you take a look at the source, both methods actually call the same implementation method:

public static System.Configuration.Configuration OpenMappedExeConfiguration(ExeConfigurationFileMap fileMap, ConfigurationUserLevel userLevel)
{
    return OpenExeConfigurationImpl(fileMap, false, userLevel, null);
}

public static System.Configuration.Configuration OpenExeConfiguration(string exePath)
{
    return OpenExeConfigurationImpl(null, false, ConfigurationUserLevel.None, exePath);
}

So when to use one over the other? Use OpenMappedExeConfiguration when you don't want to open the default configuration file. In my experience, the only time I call either of these methods is when I want to read a non-default configuration so I've only used OpenMappedExeConfiguration.