Click here to Skip to main content
11,487,406 members (69,428 online)
Click here to Skip to main content

Tagged as

DLL with configuration file

, 22 May 2011 CPOL 53.9K 35
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 = 
// Get the appSettings section
AppSettingsSection myDllConfigAppSettings = 
// return the desired field 
return myDllConfigAppSettings.Settings["MyAppSettingsKey"].Value; 

Your configuration file should look like this:
<?xml version="1.0" encoding="utf-8" ?>
    <add key ="MyAppSettingsKey" value="MyValue"/>

That's all. Hope you enjoy this tip.


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


About the Author

Ireland Ireland
I'm a Brazilian senior system analyst living in Limerick, Ireland. I've been working with software development since 1996.

Comments and Discussions

QuestionThanks for the straight-to-the-point instructions, it helped me a lot! Pin
Member 124954625-May-14 6:20
memberMember 124954625-May-14 6:20 
AnswerRe: Thanks for the straight-to-the-point instructions, it helped me a lot! Pin
fmsalmeida12-Nov-14 4:53
memberfmsalmeida12-Nov-14 4:53 
QuestionNot working when updating config file values Pin
xyz from Bangalore20-Oct-13 19:50
memberxyz from Bangalore20-Oct-13 19:50 
AnswerRe: Not working when updating config file values Pin
fmsalmeida12-Nov-14 4:57
memberfmsalmeida12-Nov-14 4:57 
GeneralRe: Not working when updating config file values Pin
Member 1061559423-Apr-15 10:33
memberMember 1061559423-Apr-15 10:33 
GeneralMy vote of 5 Pin
remi22211-Jul-13 8:27
memberremi22211-Jul-13 8:27 
QuestionNot work in WebApplication [modified] Pin
Mlsoun1-Oct-12 3:04
memberMlsoun1-Oct-12 3:04 
AnswerRe: Not work in WebApplication Pin
Fregate19-Feb-13 17:03
memberFregate19-Feb-13 17:03 
GeneralRe: Not work in WebApplication Pin
fmsalmeida20-Feb-13 1:12
memberfmsalmeida20-Feb-13 1:12 
GeneralRe: Not work in WebApplication Pin
Fregate20-Feb-13 13:15
memberFregate20-Feb-13 13:15 
GeneralRe: Not work in WebApplication Pin
eng_george128-Jan-14 2:30
membereng_george128-Jan-14 2:30 
GeneralRe: Not work in WebApplication Pin
Member 348500310-Apr-15 5:33
memberMember 348500310-Apr-15 5:33 
GeneralRe: Not work in WebApplication Pin
Member 112799398-May-15 8:25
memberMember 112799398-May-15 8:25 
QuestionVS10 Saves Setting to Project.dll.config Pin
Member 462571327-Mar-12 6:24
memberMember 462571327-Mar-12 6:24 
I had an interesting experience trying to implement this in a VB project. I renamed the app.config to P8Library.dll.config and even though it had been renamed, all the settings were still being saved to it. That's a nice feature because I can use VS2010's Settings panel to set all the default application settings and since it's in the standard format, it encrypts easily. I had to change the code to read it since it appears to .Net runtime as a ClientSettingsSection, not as ApplicationSettings.

One other thing when dealing with configuration files and trying to get your settings out of it--if you are using connection strings, they are stored in a seperate part of the app.config. Below is the code I ended up using:

' Load up the Configuration Object and encrypt if necessary

Dim cfg As Configuration = ConfigurationManager.OpenExeConfiguration(My.Application.Info.DirectoryPath & "\P8Library.dll")
Dim AppCfg As ClientSettingsSection = cfg.GetSection("applicationSettings/P8Library.My.MySettings")

For Each st As SettingElement In AppCfg.Settings
Dim value As String = st.Value.ValueXml.InnerText
Dim name As String = st.Name
AppSettings.Add(name, value)

If Not AppCfg.SectionInformation.IsProtected Then
AppCfg.SectionInformation.ForceSave = True
End If

Dim section As ConnectionStringsSection = cfg.GetSection("connectionStrings")
For Each cn As ConnectionStringSettings In section.ConnectionStrings
AppSettings.Add(cn.Name, cn.ConnectionString)

If Not section.SectionInformation.IsProtected Then
section.SectionInformation.ForceSave = True
End If


In the main program I declared a dictionary AppSettings(Of string, string) and used that to get my settings from. I did have to mark the P8Library.dll.config file as Content and include it in the installer as the author noted.
GeneralReason for my vote of 5 This solved a problem that I've been... Pin
jtdavies8-Feb-12 9:54
memberjtdavies8-Feb-12 9:54 
GeneralReason for my vote of 5 Nice, I like it, and I am bookmarkin... Pin
FDW3-Nov-11 2:06
memberFDW3-Nov-11 2:06 
GeneralReason for my vote of 5 It is very good :) Pin
Caner Korkmaz23-Aug-11 1:14
memberCaner Korkmaz23-Aug-11 1:14 
GeneralReason for my vote of 5 Bookmarked for future ref. Pin
KeithAMS2-Jun-11 23:40
memberKeithAMS2-Jun-11 23:40 
QuestionPossible in .NET 1.1? Pin
KeithAMS28-Oct-11 0:28
memberKeithAMS28-Oct-11 0:28 
AnswerRe: Possible in .NET 1.1? Pin
fmsalmeida28-Oct-11 4:18
memberfmsalmeida28-Oct-11 4:18 
GeneralGood Tip Pin
KeithAMS2-Jun-11 23:40
memberKeithAMS2-Jun-11 23:40 
GeneralRe: Good Tip Pin
fmsalmeida3-Jun-11 3:32
memberfmsalmeida3-Jun-11 3:32 
GeneralExplain why.... Pin
EngleA27-May-11 4:28
memberEngleA27-May-11 4:28 
GeneralRe: Explain why.... Pin
fmsalmeida27-May-11 5:16
memberfmsalmeida27-May-11 5:16 
QuestionToo complex? Pin
Riz Thon23-May-11 21:03
memberRiz Thon23-May-11 21:03 
AnswerRe: Too complex? Pin
fmsalmeida24-May-11 5:10
memberfmsalmeida24-May-11 5:10 

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 | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 22 May 2011
Article Copyright 2011 by fmsalmeida
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid