Introduction

There are a lot of articles that talk about the App.config file for .NET applications, be them Console applications, WinForms applications or Services, not only in CodeProject but all over the internet.

They might all look a like; but the bottom line is that none of them are the same. That's why I'm sure this article won't be the same as all of the other ones you have found and read. Who knows it might help you in some way, or even better you might get some ideas and improve it.

That was my case, I was looking for a custom, general way that I could use to read my App.config, but none of the articles I read convinced me nor solved the need I had at the time but they sure gave me good ideas of what I wanted in my own implementation.

Other people's ideas

There are two main ideas I took from other people in this open source site (of course I'm talking about CodeProject).

Paul Haley wrote in his "Enhanced AppSettings Configuration Handler" article that there are times when you need to try out your application in different environments with different configurations, and editing or commenting code for this matter can be rather annoying. Besides, the App.config is a file where all the configuration should be. (He explains more disadvantages.)

Anyway, his approach is a little more hostname oriented, and if you want to change this, you must inherit from his class and do some other stuff I didn't bother to read.

Diego Mijelshon has an article on "How to make AppSettings work with multiple values for a key". Now, you must agree with me that this is also a good idea. He states clearly that .NET has this serious flaw and he kind of explains the reason. He also shows how to get multiple values for a key, but I think he complicated it a little bit using reflection (not that reflection is hard) for reasons I am not aware of.

I must point out that there is nothing wrong with both articles, they work fine, but my needs were different, so I thought these would be too complicated and I was correct.

Background

It is recommended, of course, to know about appSettings, the System.Configuration.ConfigurationSettings class, the App.config file, and XML in which this file is coded. These subjects are rather easy and I guess most [.NET] programmers must know or at least must have heard about them, right?

OK, with this stated... let's get it on!

Main idea

As you might have read, you can get simple values inside the App.config using ConfigurationSettings.AppSettings[...] or you can create and read custom sections using the ConfigurationSettings.GetConfig(string sectionName) method, but if you want to read your config file by yourself, you have to create a class that implements the IConfigurationSectionHandler interface and this is very easy to do, for example:

publicclass AppSettingHandler : IConfigurationSectionHandler

Doing this forces you to implement a method inside your class, the Create(...) method, which basically gives you the whole App.config file already in an XmlNode called section. Inside the method, you can read the values in any way you want to.

Before I continue, I would like to point out that it is not necessary to remove the appSettings section, this is just if you want to put your configuration inside the <appSettings> tag. You can have something like this instead:

The example you get

As you can see from the line called from the code, you receive in return an ArrayList. Its structure will be shown in the following example. If you want further details, don't hesitate to download the source code.

If you process this App.config with the sample project, you'll get something like this:

Some last notes

The <Configs> node serves as the section collection.

You can put as many <Configs> nodes as you want.

You can put as many element nodes as you want.

You can see all nodes have a use attribute that can take true or false, telling the code if it will be skipped or not.

A Hashtable is used for each element node, so if you repeat them they will be ignored.

A DictionaryEntry is used to allow multiple values for the same key.

Points of Interest

Doing your own implementation of a AppSettings reader is not as hard as you might think. Of course this depends on how much you want to do, but reading the XmlNode was pretty easy and straightforward. You receive an ArrayList with Hashtables plus ArrayLists plus DictionaryEntries. I know it might sound complicated but searching within is easy and can be done with foreachs, just as it's shown in the sample project.

If you bother to look at the code, it looks pretty easy, don't you think? This worked for me, and it might work for you too, so take a look.

In resume, this helps you use multiple configurations for your projects by just turning them ON or OFF by changing the use attribute values and you can also insert multiple values for the same key.

It's been a while since i did this demo project, it only works for .NET 1.1, so make sure thats the version you are using.
For .NET 2.0, things changed, I'm not so sure what should need to be done to accomplish something like this.