Click here to Skip to main content
15,881,801 members
Articles / Programming Languages / XML
Article

Custom app.config

Rate me:
Please Sign up or sign in to vote.
4.67/5 (27 votes)
28 Mar 20052 min read 342.7K   4.6K   56   7
Custom configuration class to load configuration settings from an external file.

Introduction

This article describes how to read information from a config file exactly like the app.config file, including information on linking in external files, using custom configuration section handlers, sections, and section groups.

Background

I have looked at several articles that build everything from preference sections to simply building something that reads a section from XML that looks like the appSettings. What I really needed was a way to move settings into and out of the app.config file. I needed it to do two very specific things that none of the others did. First, I needed it to use custom configuration section handlers, and second, I needed it to assume the file name based on the assembly name because I didn't want to have to track the names of files.

Using the code

The code is used just like the System.ConfigurationSettings class in .NET, just from a different namespace.

C#
string val = 
  CustomConfigurationSettings.ConfigurationSettings.AppSettings["myTestKey"];

This is the simplest way to use it. Using it this way assumes a config file named assemblyname.dll.config or executable.exe.config, really it just picks up the output file name for the assembly and adds .config to it. The file should be in the same directory as the assembly. It supports all the built-in classes in .NET that support the IConfigurationSectionHandler interface and uses them to read all sections of the config file. It also supports using external files for appSettings just like the app.config does.

XML
<appSettings file="filename.config">
    <add key="test" value="myTest"/>
</appSettings>

If you don't want to use the appSettings section, or you want to use a different file name then the one that is assumed, it works just like the System.ConfigurationSettings class, except that the ConfigurationSettings.GetConfig is overloaded to support passing a file name that can be whatever you want as long as the format of the file matches the app.config.

C#
//Include whatever file name that you want for the second parameter
NameValueCollection values = (NameValueCollection)
  CustomConfigurationSettings.ConfigurationSettings.GetConfig("myconfigSection", 
  "myConfig.config");

string val = values["myTestKey"];

Points of Interest

Loading the external file for the AppSettings looked like it was going to be easy, just use the NameValueFileSectionHandler and that should do it, right? Well, not so fast! It didn't work. It was always returning null. I looked at what it was doing internally using Reflector and it looked pretty simple, but it was using a class marked as internal so I couldn't use that to reproduce the code. It wasn't that hard to just get the file name and load it myself, and then use the same logic that I was using on the first file, then set the parent context for the NameValueSectionHandler which combines the two sets of information into the section handler.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Software developer for 10 years, started in C++ and moved to C# about 3 years ago.

Comments and Discussions

 
GeneralappConfig file Pin
nageshn27@gmail.com19-Jun-08 3:06
nageshn27@gmail.com19-Jun-08 3:06 
Generalapp.config problem Pin
Shuaib wasif khan27-May-08 9:55
Shuaib wasif khan27-May-08 9:55 
how to add System.Machine name Value Here Sleepy | :zzz:

<add key="con" value="server=Enviornment.Machinename\INFOSOFT\ist;database=sm;user id=sa;pwd=****" />
QuestionHow can it work within web service or asp.net application? Pin
colacat31-May-06 19:32
colacat31-May-06 19:32 
GeneralCallingAssembly file name for default config file is confusing. Pin
Michael Freidgeim15-Nov-05 15:33
Michael Freidgeim15-Nov-05 15:33 
GeneralRe: CallingAssembly file name for default config file is confusing. Pin
Brian ONeil20-Nov-05 7:57
Brian ONeil20-Nov-05 7:57 
GeneralMinor suggesions for overload with file name. Pin
Michael Freidgeim3-Apr-05 18:51
Michael Freidgeim3-Apr-05 18:51 
GeneralRe: Minor suggesions for overload with file name. Pin
Brian ONeil4-Apr-05 4:13
Brian ONeil4-Apr-05 4:13 

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.