Click here to Skip to main content
Click here to Skip to main content

DLL with configuration file

, 22 May 2011
Rate this:
Please Sign up or sign in to vote.
Using configuration files in DLL is not trivial, but is very simple.
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:
//Open the configuration file using the dll location
Configuration myDllConfig = 
       ConfigurationManager.OpenExeConfiguration(this.GetType().Assembly.Location);
// Get the appSettings section
AppSettingsSection myDllConfigAppSettings = 
       (AppSettingsSection)myDllConfig.GetSection("appSettings");
// return the desired field 
return myDllConfigAppSettings.Settings["MyAppSettingsKey"].Value; 
 
Your configuration file should look like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key ="MyAppSettingsKey" value="MyValue"/>
  </appSettings>
</configuration>
 
That's all. Hope you enjoy this tip.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

fmsalmeida

Brazil Brazil
I'm a Brazilian senior system analyst and team leader and I live in Dublin, Ireland. I've been working with software development since 1996.

Comments and Discussions

 
GeneralReason for my vote of 5 Nice, I like it, and I am bookmarkin... PinmemberFDW3-Nov-11 1:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 22 May 2011
Article Copyright 2011 by fmsalmeida
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid