Thursday, February 10, 2011

Custom Configuration Section in .NET Apps

In this Blog post I am going to write how to define Custom Configuration section in .NET Apps either App.Config or Web.config  and how to use it our programs

Here are the Steps we need to perform

1)  First Create the class which is derived from ConfigurationSection this class resides inside System.Configuration Namespace.

2) Next we need Define Our custom configuration section in our App.config or Web.Config file.  for example here is my custom configuration looks like .


3) Next we need to create two more classes which Reads our configuration Key and Key collection

    Here Key are Name and Value




Next we need to Read Key collection so here is how the class for that


Here we are creating Indexer to store key collection as object.



Here is how we use it our program







1 comment:

Raj Nagalingam said...

There is better way of managing Configuration in .NET using Cinchoo Framework.

Just define a configuration class as below

#region NameSpaces

using System;
using Cinchoo.Core.Configuration;

#endregion NameSpaces

public class SampleConfigSection : ChoConfigurableObject
#region Instance Data Members (Public)

[ChoPropertyInfo("name", DefaultValue="Mark")]
public string Name;

[ChoPropertyInfo("message", DefaultValue="Hello World!")]
public string Message;


Then instantiate it and use it as below,

class Program
static void Main(string[] args)
SampleConfigSection sampleConfigSection = new SampleConfigSection();

//Shutdown the framework to stop the background services...otherwise the application will not terminate

Corresponding entries will be created in applicationexename.xml file as below

For more information, please visit