Click here to Skip to main content
11,636,778 members (76,901 online)
Click here to Skip to main content

A custom configuration file AppSettings reader class

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

Introduction

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

Background

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 = 
    StaticDust.Configuration.CustomConfigurationSettings.AppSettings(
      "C:\\yourFile.config")["yourKey"];

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 = 
   StaticDust.Configuration.CustomConfigurationSettings.AppSettings(
      configFile)["yourKey"];

History

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

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


You may also be interested in...

Comments and Discussions

 
GeneralAlternative solution... Pin
Per Hejndorf25-Sep-09 0:23
memberPer Hejndorf25-Sep-09 0:23 
Generalmy solution to dll's config Pin
Member #272780226-Feb-07 3:30
memberMember #272780226-Feb-07 3:30 
GeneralNull Pin
Zachry Baker11-Jul-05 20:25
memberZachry Baker11-Jul-05 20:25 
GeneralRe: Null Pin
jlsirera11-Nov-05 1:02
memberjlsirera11-Nov-05 1:02 
GeneralAdding key to configuration file Pin
AllenConquest13-Apr-05 22:48
memberAllenConquest13-Apr-05 22:48 
GeneralRe: Adding key to configuration file Pin
flip_trickle30-May-05 10:47
memberflip_trickle30-May-05 10:47 
GeneralNice Pin
ElNoNo15-Oct-03 4:06
memberElNoNo15-Oct-03 4:06 
GeneralRe: Nice Pin
Daniel Fisher (lennybacon)15-Oct-03 4:14
memberDaniel Fisher (lennybacon)15-Oct-03 4:14 
GeneralThere is no AppSettings(string) constructor - alternative solution Pin
Arjan Burggraaf9-Oct-03 23:24
memberArjan Burggraaf9-Oct-03 23:24 
GeneralRe: There is no AppSettings(string) constructor - alternative solution Pin
Daniel Fisher (lennybacon)10-Oct-03 8:36
memberDaniel Fisher (lennybacon)10-Oct-03 8:36 
GeneralRe: There is no AppSettings(string) constructor - alternative solution Pin
AdamFox31-May-05 10:00
memberAdamFox31-May-05 10:00 
GeneralRe: There is no AppSettings(string) constructor - alternative solution Pin
AdamFox31-May-05 10:04
memberAdamFox31-May-05 10:04 
GeneralRe: There is no AppSettings(string) constructor - alternative solution Pin
sreenivasa Rao21-Oct-03 19:39
susssreenivasa Rao21-Oct-03 19:39 
GeneralRe: There is no AppSettings(string) constructor - alternative solution Pin
Daniel Fisher (lennybacon)21-Oct-03 23:03
memberDaniel Fisher (lennybacon)21-Oct-03 23:03 

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