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

DLL with configuration file

By , 22 May 2011
 
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
Other Zurich
Brazil Brazil
Member
I'm a system analyst (senior) and I live in Belo Horizonte, Minas Gerais, Brazil. I've been working with software development since 1996.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionNot work in WebApplication [modified]memberMlsoun1 Oct '12 - 2:04 
AnswerRe: Not work in WebApplicationmemberFregate19 Feb '13 - 16:03 
GeneralRe: Not work in WebApplicationmemberfmsalmeida20 Feb '13 - 0:12 
GeneralRe: Not work in WebApplicationmemberFregate20 Feb '13 - 12:15 
QuestionVS10 Saves Setting to Project.dll.configmemberMember 462571327 Mar '12 - 5:24 
GeneralReason for my vote of 5 This solved a problem that I've been...memberjtdavies8 Feb '12 - 8:54 
GeneralReason for my vote of 5 Nice, I like it, and I am bookmarkin...memberFDW3 Nov '11 - 1:06 
GeneralReason for my vote of 5 It is very good :)memberCaner Korkmaz23 Aug '11 - 0:14 
Reason for my vote of 5
It is very good Smile | :)
GeneralReason for my vote of 5 Bookmarked for future ref.memberKeithAMS2 Jun '11 - 22:40 
QuestionPossible in .NET 1.1?memberKeithAMS27 Oct '11 - 23:28 
AnswerRe: Possible in .NET 1.1?memberfmsalmeida28 Oct '11 - 3:18 
GeneralGood TipmemberKeithAMS2 Jun '11 - 22:40 
GeneralRe: Good Tipmemberfmsalmeida3 Jun '11 - 2:32 
GeneralExplain why....memberEngleA27 May '11 - 3:28 
GeneralRe: Explain why....memberfmsalmeida27 May '11 - 4:16 
QuestionToo complex?memberRiz Thon23 May '11 - 20:03 
AnswerRe: Too complex?memberfmsalmeida24 May '11 - 4:10 

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

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