Click here to Skip to main content
15,890,897 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 195.4K   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

 
QuestionI vote 100% Pin
Member 308835528-Apr-20 10:16
Member 308835528-Apr-20 10:16 
PraiseThanks for this! Pin
jobzfirme10-May-18 8:21
jobzfirme10-May-18 8:21 
QuestionVote of 5 Pin
Shekhar Pankaj2-Feb-17 4:07
professionalShekhar Pankaj2-Feb-17 4:07 
AnswerRe: Vote of 5 Pin
fmsalmeida1-Mar-17 0:47
professionalfmsalmeida1-Mar-17 0:47 
Questionwhy use configuration files with DLLs Pin
bkelly1324-Feb-16 12:59
bkelly1324-Feb-16 12:59 
AnswerRe: why use configuration files with DLLs Pin
Garth J Lancaster24-Feb-16 13:27
professionalGarth J Lancaster24-Feb-16 13:27 
GeneralRe: why use configuration files with DLLs Pin
bkelly1325-Feb-16 13:03
bkelly1325-Feb-16 13:03 
GeneralRe: why use configuration files with DLLs Pin
Garth J Lancaster25-Feb-16 14:02
professionalGarth J Lancaster25-Feb-16 14:02 
GeneralRe: why use configuration files with DLLs Pin
Garth J Lancaster25-Feb-16 14:33
professionalGarth J Lancaster25-Feb-16 14:33 
2nd reply - from this site Nini: Manual[^]

Quote:
1.1 What is Application Configuration Data?

As a developer you deal with application configuration data all of the time. Common examples of this are INI files, XML files, .NET configuration files (aka “.config”), the Windows registry, and command line (argv) arguments. The advantages of configuration files are that they load quickly, do not take up a lot of space, and are easy to edit.

1.2 The Problem

Attempts to create configuration file access schemes do not satisfy the needs of either programmers or end-users. To give a real life scenario I worked for an organization that configured their original programs using the Windows registry API (Application Programming Interface). Later on they developed their own ASP configuration class. At about the same time another group developed an API that fetched the data from a database. Then when ASP.NET came along they started to use Web.config. In a matter of several years the number of configuration data sources grew from one to four! Needless to say getting configuration data often became a grueling task. Here are the three major areas where configuration management can be improved:

API
Developers use a configuration file format that gets their application running in the shortest time possible. However the API for accessing this data is commonly added as an afterthought resulting in an inflexible API. In very small applications this might not be a problem but as a program’s codebase grows the configuration information will often find itself littered throughout the application code.

End Users
Configuration files are usually not written with the end user in mind. Often the configuration options are terse programming terms that only the bravest users dare to change them. This leads to developers having to write complicated configuration file editors or worse, entirely redesigning their original APIs.

Multiple Configuration Sources
As your software matures it is not uncommon for more application configuration types to be added (such as the example I gave you earlier). This often occurs because of merging code from other projects, new improved formats, and moving to different programming platforms. This forces programmers to learn multiple APIs. The end result is code that is neither consistent nor friendly to new programmers. The old configuration files aren't replaced because programmers and their managers are not comfortable with altering mature code. Users that edit the files are resistant to this change because they would prefer not to learn a new file format.


and

Quote:
5.5 Choosing the right configuration file type for your application

Nini was written to make all configuration file types first-class citizens. This is because each configuration file type has it's own strengths and weaknesses. The list below contains some basic guidelines:

INI
Speed - The file type is parsed very quickly so it is likely to load and save faster than other types.
Readibility - Of the configuration types this is probably the least scary to users. If you are going to have users alter this configuration file manually then I greatly recommend this format.
Installers - INI files are supported by many types of installers (Wise, NSIS, to name a couple). If you need your build to edit these files than this is definitely the way to go.
XML
Speed - The XML parser has a lot of information to load so this is probably the slowest.
Readibility - This format is a bit scary for beginning users to understand.
Support - This is supported by all programming languages so if other applications are accessing data then this is a good type.
Installers - Since there is no standard XML format for configuration files this is not often supported by installers.
.NET Configuration File
Speed - The XML parser has a lot of information to load so this is probably the slowest.
Readibility - This format is a bit scary for beginning users to understand.
Installers - The .NET Framework uses these configuration files for other configurations. I would recommend not using it if it mixes up .NET configurations and your own application configuration options.
Windows Registry
Speed - Retrieves configuration data quickly. The retrieval speed does become slower as the registry becomes increasingly filled with data. This is cited as one of the reasons why periodically re-installing Windows can make a computer run faster.
Readibility - You should use this type if you really do not want users to be able to configure the application. Touching the Registry can cause serious problems so it is recommended that you do not use it unless necessary. However, know that your support staff will have the same problems as users if they need users to tweak configuration settings. Other than that, the configuration file editor is pretty well known and mature.
Installers - Most installers support reading and editing registry keys so this is a good choice.

There is no perfect configuration type because each one has it's own benefits. If you end up choosing a configuration file type that turns out to not right for your situation then do not be alarmed. Nini abstracts what type of file you are accessing so the amount of code that you will have to change should be minimal.

QuestionThanks for the straight-to-the-point instructions, it helped me a lot! Pin
Member 124954625-May-14 5:20
Member 124954625-May-14 5:20 
AnswerRe: Thanks for the straight-to-the-point instructions, it helped me a lot! Pin
fmsalmeida12-Nov-14 3:53
professionalfmsalmeida12-Nov-14 3:53 
QuestionNot working when updating config file values Pin
xyz from Bangalore20-Oct-13 18:50
xyz from Bangalore20-Oct-13 18:50 
AnswerRe: Not working when updating config file values Pin
fmsalmeida12-Nov-14 3:57
professionalfmsalmeida12-Nov-14 3:57 
GeneralRe: Not working when updating config file values Pin
Member 1061559423-Apr-15 9:33
Member 1061559423-Apr-15 9:33 
GeneralMy vote of 5 Pin
remi22211-Jul-13 7:27
remi22211-Jul-13 7:27 
GeneralRe: My vote of 5 Pin
fmsalmeida12-Nov-14 3:57
professionalfmsalmeida12-Nov-14 3:57 
QuestionNot work in WebApplication Pin
Mlsoun1-Oct-12 2:04
Mlsoun1-Oct-12 2:04 
AnswerRe: Not work in WebApplication Pin
Fregate19-Feb-13 16:03
Fregate19-Feb-13 16:03 
GeneralRe: Not work in WebApplication Pin
fmsalmeida20-Feb-13 0:12
professionalfmsalmeida20-Feb-13 0:12 
GeneralRe: Not work in WebApplication Pin
Fregate20-Feb-13 12:15
Fregate20-Feb-13 12:15 
GeneralRe: Not work in WebApplication Pin
eng_george128-Jan-14 1:30
professionaleng_george128-Jan-14 1:30 
GeneralRe: Not work in WebApplication Pin
Member 348500310-Apr-15 4:33
Member 348500310-Apr-15 4:33 
GeneralRe: Not work in WebApplication Pin
Member 112799398-May-15 7:25
Member 112799398-May-15 7:25 
QuestionVS10 Saves Setting to Project.dll.config Pin
Member 462571327-Mar-12 5:24
Member 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
jtdavies8-Feb-12 8:54 

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.