DLL with configuration file

Share on TwitterShare on TumblrSubmit to StumbleUponSave on DeliciousDigg This

Using configuration files in DLL is not trivial.
To accomplish this, we must follow these steps:

  • Add a reference to System.Configuration
  • Add a new Application Configuration File and the name it [Your project].dll.config
  • Change the BuildAction property of the config file to Content
  • Change the Copy to Output property to “Copy Always

After these steps, you can access the configuration file this way:

Collapse | Copy Code
//Open the configuration file using the dll location
Configuration myDllConfig = 
// Get the appSettings section
AppSettingsSection myDllConfigAppSettings = 
// return the desired field 
return myDllConfigAppSettings.Settings["MyAppSettingsKey"].Value;

Your configuration file should look like this:

Collapse | Copy Code
<?xml version="1.0" encoding="utf-8" ?>
    <add key ="MyAppSettingsKey" value="MyValue"/>

That’s all. Hope you enjoy this tip.