Click here to Skip to main content
12,633,336 members (33,071 online)
Click here to Skip to main content
Add your own
alternative version

Stats

107.4K views
418 downloads
35 bookmarked
Posted

A custom configuration file AppSettings reader class

, 21 Oct 2003 CPOL
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

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.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralRe: There is no AppSettings(string) constructor - alternative solution Pin
AdamFox31-May-05 11:00
memberAdamFox31-May-05 11:00 
GeneralRe: There is no AppSettings(string) constructor - alternative solution Pin
AdamFox31-May-05 11:04
memberAdamFox31-May-05 11:04 
GeneralRe: There is no AppSettings(string) constructor - alternative solution Pin
sreenivasa Rao21-Oct-03 20:39
susssreenivasa Rao21-Oct-03 20:39 
GeneralRe: There is no AppSettings(string) constructor - alternative solution Pin
Daniel Fisher (lennybacon)22-Oct-03 0:03
memberDaniel Fisher (lennybacon)22-Oct-03 0:03 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161208.2 | Last Updated 22 Oct 2003
Article Copyright 2003 by Daniel Fisher (lennybacon)
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid