18

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"];
Kevin Driedger
  • 51,492
  • 15
  • 48
  • 55
  • Possible duplicate of [How can I read/write app.config settings at runtime without using user settings?](https://stackoverflow.com/questions/3638754/how-can-i-read-write-app-config-settings-at-runtime-without-using-user-settings) – Matt Jul 05 '17 at 15:48

5 Answers5

23

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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    What if I don't have appsettings node in my custom config file ? I have node. So how to get the nodes without using AppSettingsSection ? – Shesha Sep 21 '16 at 04:25
  • 1
    There's something else you should know: unless you pass `ExeConfigFilename` a _full path_, you are going to get a file stored in `C:\Windows\System32`, and _not_ in your app directory, as you might expect. – DonBoitnott Mar 27 '19 at 14:00
7
var appSettings = ConfigurationManager.OpenExeConfiguration((Assembly.GetAssembly(typeof(MYASSEMBLY))).Location).AppSettings;

then you can do as above.

Taryn
  • 242,637
  • 56
  • 362
  • 405
Fx Mzt
  • 381
  • 3
  • 5
0
var uri = new Uri(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase));
var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = Path.Combine(uri.LocalPath, "MyAssembly.dll.config") };
var assemblyConfig = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None); 
Kevin Driedger
  • 51,492
  • 15
  • 48
  • 55
  • not really - you will get a "Configuration" object from the OpenMappedExeCOnfiguration call, and if you use that, then you peek inside your mapped config file, but if you use teh `ConfigurationManager.AppSettings[]`, you're still getting the main app's configs. – marc_s Nov 05 '09 at 17:08
0

You can also open it up as an XmlDocument and navigate the document with Xpath. THen there is always LinqToXml

Chuck Conway
  • 16,287
  • 11
  • 58
  • 101
-1
Using System.Configuration
Public Shared Function AppDomainConfiguration() As Configuration
  Dim fileMap As New ExeConfigurationFileMap
  fileMap.ExeConfigFilename = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile
  Return ConfigurationManager.OpenMappedExeConfiguration(fileMap,Configuration.ConfigurationUserLevel.None)
End Function
Alberto De Caro
  • 5,147
  • 9
  • 47
  • 73
David
  • 35
  • 3