Click here to Skip to main content
12,945,945 members (40,682 online)
Click here to Skip to main content
Add your own
alternative version

Stats

90.4K views
38 bookmarked
Posted 20 May 2011

DLL with configuration file

, 22 May 2011 CPOL
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)

Share

About the Author

fmsalmeida
Software Developer (Senior) ResourceKraft
Ireland Ireland
I'm a Brazilian senior system analyst living in Limerick, Ireland. I've been working with software development since 1996.

You may also be interested in...

Comments and Discussions

 
GeneralRe: Not work in WebApplication Pin
Member 348500310-Apr-15 4:33
memberMember 348500310-Apr-15 4:33 
GeneralRe: Not work in WebApplication Pin
Member 112799398-May-15 7:25
memberMember 112799398-May-15 7:25 
QuestionVS10 Saves Setting to Project.dll.config Pin
Member 462571327-Mar-12 5:24
memberMember 462571327-Mar-12 5:24 
GeneralReason for my vote of 5 This solved a problem that I've been... Pin
jtdavies8-Feb-12 8:54
memberjtdavies8-Feb-12 8:54 
GeneralReason for my vote of 5 Nice, I like it, and I am bookmarkin... Pin
FDW3-Nov-11 1:06
memberFDW3-Nov-11 1:06 
GeneralReason for my vote of 5 It is very good :) Pin
Caner Korkmaz23-Aug-11 0:14
memberCaner Korkmaz23-Aug-11 0:14 
GeneralReason for my vote of 5 Bookmarked for future ref. Pin
KeithAMS2-Jun-11 22:40
memberKeithAMS2-Jun-11 22:40 
QuestionPossible in .NET 1.1? Pin
KeithAMS27-Oct-11 23:28
memberKeithAMS27-Oct-11 23:28 
Hi

I have to write an app in .NET 1.1, any idea if this style of method can be used there? I can't figure it out so far! Annoying because it was all working nicely in .NEt 2 before I realised I had to backwardly port it.

Thanks!
KM

I'm pink therefore I'm Spam

AnswerRe: Possible in .NET 1.1? Pin
fmsalmeida28-Oct-11 3:18
memberfmsalmeida28-Oct-11 3:18 
GeneralGood Tip Pin
KeithAMS2-Jun-11 22:40
memberKeithAMS2-Jun-11 22:40 
GeneralRe: Good Tip Pin
fmsalmeida3-Jun-11 2:32
memberfmsalmeida3-Jun-11 2:32 
GeneralExplain why.... Pin
EngleA27-May-11 3:28
memberEngleA27-May-11 3:28 
GeneralRe: Explain why.... Pin
fmsalmeida27-May-11 4:16
memberfmsalmeida27-May-11 4:16 
QuestionToo complex? Pin
Riz Thon23-May-11 20:03
memberRiz Thon23-May-11 20:03 
AnswerRe: Too complex? Pin
fmsalmeida24-May-11 4:10
memberfmsalmeida24-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170518.1 | Last Updated 22 May 2011
Article Copyright 2011 by fmsalmeida
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid