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

A custom configuration file AppSettings reader class

, 21 Oct 2003
Rate this:
Please Sign up or sign in to vote.
This article describes how to create a custom configuration file AppSettings reader class.


I'll explain how to build a AppSettings reader class that can be used with every .config file you want.


When you deploy .dll assemblies (especially for ASP.NET applications), you are forced to use the main application's configuration file. Some of my applications have a lot of entries to add to the AppSettings section of the .config file and it is annoying to do all the modification to the configuration file to just run the application.

The code

To access the configuration file, the following class returns a custom IDictionary object:

public class CustomConfigurationSettings
    public static AppSettingsReader AppSettings(string configFile)
        return new AppSettingsReader(configFile);

You can call it in your code like this:

object settingsValue = 

To get the file named {yourAssembly}.config in a web application, call it as follows:

Assembly assmebly = Assembly.GetExecutingAssembly();
string configFile = 
    System.Web.HttpContext.Current.Request.PhysicalApplicationPath + 
      "bin\\" + assmebly.GetName().ToString().Split(',')[0] + ".config";

object settingsValue = 


  • 10/04/2003 v.
  • 10/22/2003 Changed class name to CustomConfigurationSettings.


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

Daniel Fisher (lennybacon)
Software Developer
Germany Germany
Beginning of the nineties started to assemble computers and configure networks. Automation lead to batching and scripting. Arrived on the other side of the trench the HTTP protocol is a constant companion. the journey began with JavaScript, then Perl, PHP and ASP with Visual Basic 5 and JScript, ending with Java and C++. End of the nineties starting to focus .NET, streaked Python, and now JavaScript again. He develops, teaches, trains, coaches and speaks. His topics: HTML5 & Web, Data Access & Performance, Scalable & Testable Designs, Distributed Systems & Services, Security & Trust.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

GeneralAlternative solution... PinmemberPer Hejndorf25-Sep-09 1:23 
Generalmy solution to dll's config PinmemberMember #272780226-Feb-07 4:30 
GeneralNull PinmemberZachry Baker11-Jul-05 21:25 
GeneralRe: Null Pinmemberjlsirera11-Nov-05 2:02 
GeneralAdding key to configuration file PinmemberAllenConquest13-Apr-05 23:48 
GeneralRe: Adding key to configuration file Pinmemberflip_trickle30-May-05 11:47 
GeneralNice PinmemberElNoNo15-Oct-03 5:06 
GeneralRe: Nice PinmemberDaniel Fisher (lennybacon)15-Oct-03 5:14 
GeneralThere is no AppSettings(string) constructor - alternative solution PinmemberArjan Burggraaf10-Oct-03 0:24 
I tried your code (see below) with .Net Framework v1.1.4322, but it does not compile. Reason: there is no AppSettingsReader constructor that accepts a string.
using System.Configuration;
public class ConfigurationSettings
public static AppSettingsReader AppSettings(string configFile)
{ return new AppSettingsReader(configFile); }
The following is kind of a workaround, but it works:
using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Reflection;
using System.Security.Policy;

public class CustomConfigurationSettings
#region --- Methods ---
public static CustomConfigurationSettings Get(string configurationFile)
AppDomainSetup setup = new AppDomainSetup();
setup.ConfigurationFile = configurationFile;
Evidence evidence = AppDomain.CurrentDomain.Evidence;
AppDomain newDomain = AppDomain.CreateDomain("customConfigurationSettings " + configurationFile, evidence, setup);

AssemblyName assemblyName = typeof(CustomConfigurationSettings).Assembly.GetName(true);
Assembly ass = newDomain.Load(assemblyName);
CustomConfigurationSettings result = (CustomConfigurationSettings)ass.CreateInstance(typeof(CustomConfigurationSettings).FullName);

return result;
#endregion // Methods

#region --- Properties ---
public NameValueCollection AppSettings
return ConfigurationSettings.AppSettings;
#endregion // Properties

#region --- Methods ---
public object GetConfig(string sectionName)
return ConfigurationSettings.GetConfig(sectionName);
#endregion // Methods
You get the appsettings as follows:
You can get a configuration section as follows:

GeneralRe: There is no AppSettings(string) constructor - alternative solution PinmemberDaniel Fisher (lennybacon)10-Oct-03 9:36 

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.150327.1 | Last Updated 22 Oct 2003
Article Copyright 2003 by Daniel Fisher (lennybacon)
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid