65.9K
CodeProject is changing. Read more.
Home

A custom configuration file AppSettings reader class

Oct 4, 2003

CPOL
viewsIcon

124335

downloadIcon

462

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.