Click here to Skip to main content
6,629,377 members and growing! (19,152 online)
Email Password   helpLost your password?
Desktop Development » Files and Folders » Configuration Files     Intermediate

A custom configuration file AppSettings reader class

By Daniel Fisher (lennybacon)

This article describes how to create a custom configuration file AppSettings reader class.
C#.NET 1.0, .NET 1.1, Win2K, WinXP, Win2003VS.NET2003, Dev
Posted:3 Oct 2003
Updated:21 Oct 2003
Views:79,513
Bookmarked:32 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
16 votes for this article.
Popularity: 3.89 Rating: 3.23 out of 5
2 votes, 12.5%
1
2 votes, 12.5%
2
3 votes, 18.8%
3
4 votes, 25.0%
4
5 votes, 31.3%
5

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

About the Author

Daniel Fisher (lennybacon)


Member
Daniel Fisher is co-founder of devcoach. He supports since 1997 customers and projects in Germany and throughout Europe. As a consultant and developer he has worked several years on projects for insurance companies, distributors, mobile communications hardware, construction supplies and various other companies of different branches. He has a strong emphasis on service-orientation, agile methods, the web and data access. He is a frequent speaker on national and international software developer conferences in Germany, England and Poland. Daniel is actively contributing the software developer community as lead of a user group and organizer of the biggest regional software developer community event. You can read his blog by visiting lennybacon.com.
Occupation: Web Developer
Location: Germany Germany

Other popular Files and Folders articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 14 of 14 (Total in Forum: 14) (Refresh)FirstPrevNext
GeneralAlternative solution... PinmemberPer Hejndorf1:23 25 Sep '09  
Generalmy solution to dll's config Pinmember4:30 26 Feb '07  
GeneralNull PinmemberZachry Baker21:25 11 Jul '05  
GeneralRe: Null Pinmemberjlsirera2:02 11 Nov '05  
GeneralAdding key to configuration file PinmemberAllenConquest23:48 13 Apr '05  
GeneralRe: Adding key to configuration file Pinmemberflip_trickle11:47 30 May '05  
GeneralNice PinmemberElNoNo5:06 15 Oct '03  
GeneralRe: Nice PinmemberDaniel Fisher (lennybacon)5:14 15 Oct '03  
GeneralThere is no AppSettings(string) constructor - alternative solution PinmemberArjan Burggraaf0:24 10 Oct '03  
GeneralRe: There is no AppSettings(string) constructor - alternative solution PinmemberDaniel Fisher (lennybacon)9:36 10 Oct '03  
GeneralRe: There is no AppSettings(string) constructor - alternative solution PinmemberAdamFox11:00 31 May '05  
GeneralRe: There is no AppSettings(string) constructor - alternative solution PinmemberAdamFox11:04 31 May '05  
GeneralRe: There is no AppSettings(string) constructor - alternative solution Pinsusssreenivasa Rao20:39 21 Oct '03  
GeneralRe: There is no AppSettings(string) constructor - alternative solution PinmemberDaniel Fisher (lennybacon)0:03 22 Oct '03  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 21 Oct 2003
Editor: Smitha Vijayan
Copyright 2003 by Daniel Fisher (lennybacon)
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project