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 .

image

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

    Here Key are Name and Value

image

image

 

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

image

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

 

 

Here is how we use it our program

image

 

image

 

Nandri(thanks)

SreenivasaRagavan

1 comment:

Cinchoo 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

[ChoConfigurationSection("sample")]
public class SampleConfigSection : ChoConfigurableObject
{
#region Instance Data Members (Public)

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

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

#endregion
}

Then instantiate it and use it as below,

class Program
{
static void Main(string[] args)
{
SampleConfigSection sampleConfigSection = new SampleConfigSection();
Console.WriteLine(sampleConfigSection.ToString());

//Shutdown the framework to stop the background services...otherwise the application will not terminate
ChoFramework.Shutdown();
}
}

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













For more information, please visit http://www.cinchoo.com