Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A custom configuration file AppSettings reader class

0.00/5 (No votes)
21 Oct 2003 3  
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