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

Custom app.config

, 28 Mar 2005
Rate this:
Please Sign up or sign in to vote.
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.

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.

    <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.

//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

Share

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 Pinmembernageshn27@gmail.com19-Jun-08 3:06 
Generalapp.config problem Pinmembermonu_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? Pinmembercolacat31-May-06 19:32 
GeneralCallingAssembly file name for default config file is confusing. PinmemberMichael Freidgeim15-Nov-05 15:33 
GeneralRe: CallingAssembly file name for default config file is confusing. PinmemberBrian ONeil20-Nov-05 7:57 
GeneralMinor suggesions for overload with file name. PinmemberMichael Freidgeim3-Apr-05 18:51 
GeneralRe: Minor suggesions for overload with file name. PinmemberBrian ONeil4-Apr-05 4:13 

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
Web01 | 2.8.141030.1 | Last Updated 29 Mar 2005
Article Copyright 2005 by Brian ONeil
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid