Click here to Skip to main content
15,881,715 members
Articles / Programming Languages / C#
Tip/Trick

DLL with configuration file

Rate me:
Please Sign up or sign in to vote.
4.79/5 (32 votes)
22 May 2011CPOL 194.1K   42   36
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
<?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)


Written By
Team Leader ActionPoint
Ireland Ireland
I'm Brazilian currently living in in Limerick, Ireland. I've been working with software development since 1996.

Comments and Discussions

 
GeneralReason for my vote of 5 It is very good :) Pin
Caner Korkmaz23-Aug-11 0:14
Caner Korkmaz23-Aug-11 0:14 
GeneralReason for my vote of 5 Bookmarked for future ref. Pin
KeithAMS2-Jun-11 22:40
KeithAMS2-Jun-11 22:40 
QuestionPossible in .NET 1.1? Pin
KeithAMS27-Oct-11 23:28
KeithAMS27-Oct-11 23:28 
AnswerRe: Possible in .NET 1.1? Pin
fmsalmeida28-Oct-11 3:18
professionalfmsalmeida28-Oct-11 3:18 
GeneralGood Tip Pin
KeithAMS2-Jun-11 22:40
KeithAMS2-Jun-11 22:40 
GeneralRe: Good Tip Pin
fmsalmeida3-Jun-11 2:32
professionalfmsalmeida3-Jun-11 2:32 
GeneralExplain why.... Pin
EngleA27-May-11 3:28
EngleA27-May-11 3:28 
GeneralRe: Explain why.... Pin
fmsalmeida27-May-11 4:16
professionalfmsalmeida27-May-11 4:16 
well, this is not an article, this is just a tip.

I had to use a configuration file for my DLL, instead of putting the configuration on each project's app.config or web.config that uses my DLL, so I started researching how to solve this, and every sample I saw was very complex, using new app domains, etc...

As I did not want to use INI files or windows registry, i used this approach, and the way I did, was very simpler and I thought it would be nice to share.

that's all
QuestionToo complex? Pin
Riz Thon23-May-11 20:03
Riz Thon23-May-11 20:03 
AnswerRe: Too complex? Pin
fmsalmeida24-May-11 4:10
professionalfmsalmeida24-May-11 4:10 

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

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