Click here to Skip to main content
15,886,026 members
Articles / Mobile Apps / Windows Mobile
Article

A custom configuration file AppSettings reader class

Rate me:
Please Sign up or sign in to vote.
3.86/5 (16 votes)
21 Oct 2003CPOL 122.8K   460   35   14
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:

C#
public class CustomConfigurationSettings
{
    public static AppSettingsReader AppSettings(string configFile)
    {
        return new AppSettingsReader(configFile);
    }
}

You can call it in your code like this:

C#
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:

C#
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)


Written By
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.

Comments and Discussions

 
GeneralAlternative solution... Pin
Per Hejndorf25-Sep-09 0:23
Per Hejndorf25-Sep-09 0:23 
Generalmy solution to dll's config Pin
J Xie26-Feb-07 3:30
J Xie26-Feb-07 3:30 
GeneralNull Pin
Member 84878311-Jul-05 20:25
Member 84878311-Jul-05 20:25 
GeneralRe: Null Pin
jlsirera11-Nov-05 1:02
professionaljlsirera11-Nov-05 1:02 
GeneralAdding key to configuration file Pin
AllenConquest13-Apr-05 22:48
AllenConquest13-Apr-05 22:48 
GeneralRe: Adding key to configuration file Pin
flip_trickle30-May-05 10:47
flip_trickle30-May-05 10:47 
AllenConquest wrote:
How do I add a new key and save this back to the config file?

Something like this would require writing to the file which seemsto be a bit out of the scope of this artile (it's a great article and code btw). The authors code is pretty straight forward (clear and concice) and reads the contents of the standard <appSettings> node (see msdn schema) from any config file using the DictionarySectionHandler which to me seem designed for read-only access.

So in the code from this article there is no way to add key value pairs and write to the config file. But some investigation into serializing the IDictionary object to file in xml form would probably be a good place to start. Some articles have been writen about access to .config files using the System.Xml.Serialization namespace which would give you much more power for writing to the file, you might want to check outthis one.

Hope this helps.

Cheers,

- flip
GeneralNice Pin
<marquee>ElNoNo15-Oct-03 4:06
<marquee>ElNoNo15-Oct-03 4:06 
GeneralRe: Nice Pin
Daniel Fisher (lennybacon)15-Oct-03 4:14
Daniel Fisher (lennybacon)15-Oct-03 4:14 
GeneralThere is no AppSettings(string) constructor - alternative solution Pin
Arjan Burggraaf9-Oct-03 23:24
Arjan Burggraaf9-Oct-03 23:24 
GeneralRe: There is no AppSettings(string) constructor - alternative solution Pin
Daniel Fisher (lennybacon)10-Oct-03 8:36
Daniel Fisher (lennybacon)10-Oct-03 8:36 
GeneralRe: There is no AppSettings(string) constructor - alternative solution Pin
AdamFox31-May-05 10:00
AdamFox31-May-05 10:00 
GeneralRe: There is no AppSettings(string) constructor - alternative solution Pin
AdamFox31-May-05 10:04
AdamFox31-May-05 10:04 
GeneralRe: There is no AppSettings(string) constructor - alternative solution Pin
jsrao21-Oct-03 19:39
jsrao21-Oct-03 19:39 
GeneralRe: There is no AppSettings(string) constructor - alternative solution Pin
Daniel Fisher (lennybacon)21-Oct-03 23:03
Daniel Fisher (lennybacon)21-Oct-03 23: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.