Click here to Skip to main content
14,576,970 members

Custom app.config

Rate this:
4.67 (27 votes)
Please Sign up or sign in to vote.
4.67 (27 votes)
28 Mar 2005
Custom configuration class to load configuration settings from an external file.


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.


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.

string val = 

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.

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

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.

//Include whatever file name that you want for the second parameter
NameValueCollection values = (NameValueCollection)

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.


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


About the Author

Brian ONeil
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
Membernageshn27@gmail.com19-Jun-08 3:06 
Generalapp.config problem Pin
Shuaib wasif khan27-May-08 9:55
MemberShuaib wasif khan27-May-08 9:55 
QuestionHow can it work within web service or application? Pin
colacat31-May-06 19:32
Membercolacat31-May-06 19:32 
GeneralCallingAssembly file name for default config file is confusing. Pin
Michael Freidgeim15-Nov-05 15:33
MemberMichael Freidgeim15-Nov-05 15:33 
GeneralRe: CallingAssembly file name for default config file is confusing. Pin
Brian ONeil20-Nov-05 7:57
MemberBrian ONeil20-Nov-05 7:57 
GeneralMinor suggesions for overload with file name. Pin
Michael Freidgeim3-Apr-05 18:51
MemberMichael Freidgeim3-Apr-05 18:51 
GeneralRe: Minor suggesions for overload with file name. Pin
Brian ONeil4-Apr-05 4:13
MemberBrian 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.

Posted 28 Mar 2005


54 bookmarked