How do I retrieve AppSettings from the assembly config file?

.net, app-config, c#

Solution

Using the `OpenMappedExeConfiguration` gives you back a "Configuration" object which you can use to peek into the class library's config (and the settings that exist there will override the ones by the same name in the main app's config):

ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = "ConfigLibrary.config";

Configuration libConfig = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);

AppSettingsSection section = (libConfig.GetSection("appSettings") as AppSettingsSection);
value = section.Settings["Test"].Value;

But those settings that are unique to the main app's config and do not exist in the class library's own config are still accessible via the `ConfigurationManager` static class:

string serial = ConfigurationManager.AppSettings["Serial"];

That still works - the class library's config only hides those settings that are inside its config file; plus you need to use the "`libConfig` instance to get access to the class library's own config settings, too .

The two worlds (main app.config, classlibrary.config) can totally and very happily co-exist - not a problem there at all!

Marc

Problem

I would like to retrieve the AppSetting key from the assembly config file called: MyAssembly.dll.config. Here's a sample of the config file: ``` <configuration> <appSettings> <add key="MyKey" value="MyVal"/> </appSettings> </configuration> ``` Here's the code to retrieve it: ``` var myKey = ConfigurationManager.AppSettings["MyKey"]; ```

Original source

Related problems