Click here to Skip to main content
15,119,363 members
Articles / Web Development / ASP.NET
Posted 14 Jul 2005


163 bookmarked

Understanding Section Handlers - App.config File

Rate me:
Please Sign up or sign in to vote.
4.51/5 (56 votes)
15 Jul 20054 min read
This article explains configuration section handlers defined in the System.Configuration namespace and explains how to create custom sections handlers by implemeting the IConfigurationSectionHandler interface.


This article explains configuration section handlers defined in the System.Configuration namespace and explains how to create custom section handlers by implementing the interface IConfigurationSectionHandler.

What we can do with App.config?

A configuration file can contain information that the application reads at run time. We can use configuration sections to specify this information in configuration files. The .NET Framework provides several predefined configuration sections and developers can also create custom configuration sections.

Configuration sections have two parts: a configuration section declaration and the configuration settings. We can put configuration section declarations and configuration settings in the machine configuration file or in the application configuration file. At run time, section handlers, which are classes that implement the IConfigurationSectionHandler interface, read settings in the machine configuration file first, followed by the application configuration file. Depending on the section handler, either the settings in the application file override the settings in the machine file or the settings from both files are merged.

Note: It’s not a good practice to specify application settings in machine.config file.

There are two types of configuration sections:

  • Predefined configuration section (appSettings).
  • User-Defined configuration section(s).

Predefined configuration Section (appSettings)

The .NET Framework provides a predefined configuration section called appSettings. This section is declared in the Machine.config file as follows:

<section name="appSettings" 
      System, Version=1.0.5000.0, Culture=neutral, 

The sample declaration of the appSettings is:

<appSettings file="appSettings.xml"/>

Application settings are defined in an external file, which should be in the application bin folder.

A sample appSettings.xml is:

<?xml version="1.0" encoding="utf-8"?> 
    <add key="article" value="Configuration Sections"/>
    <add key="author" value="Palanisamy Veerasingam"/>

or we can keep the appSettings in the App.config file itself:

    <add key="author" value="Palanisamy Veerasingam"/>
    <add key="article" value="Configuration Sections"/>   

ConfigurationSettings.AppSettings is a special property that provides a shortcut to application settings defined in the <appSettings> section of the configuration file. The following example shows how to retrieve the author name defined in the previous configuration section example:

public string GetAuthor()
    return (ConfigurationSettings.AppSettings["author"]);

User-Defined configuration sections or Custom configuration sections

Declaring custom configuration sections

    <section name="sampleSection"
    type="System.Configuration.SingleTagSectionHandler" />

The <section> element has two properties:

  • The name attribute, which is the name of the element that contains the information the section handler reads.
  • The type attribute, which is the name of the class that reads the information.

We can specify the following values to this type attribute:

  • System.Configuration.NameValueSectionHandler – Provides name-value pair configuration information from a configuration section and returns System.Collections.Specialized.NameValueCollection object.
  • System.Configuration.DictionarySectionHandler - Reads key-value pair configuration information for a configuration section and returns System.Collections.Hashtable object.
  • System.Configuration.SingleTagSectionHandler - Reads key-value pair configuration information for a configuration section and returns the System.Collections.Hashtable object.
  • System.Configuration.IgnoreSectionHandler - Provides a section handler definition for configuration sections read and handled by systems other than System.Configuration. To access this section, we have to parse the whole App.config XML file.
  • User defined section handlers, which implement System.Configuration.IConfigurationSectionHandler.

Also, we can use <sectionGroup> tags for more detailed declaration of our sections as follows:

<sectionGroup name="mainGroup">
<sectionGroup name="subGroup">
    <section ...

I hope that by looking at the sample, you can understand the difference between these handlers. I not yet faced a situation to use IgnoreSectionHandler; in that case using a different file will be a good practice, I think so.

Declaring user-defined section handlers

To declare a user-defined section handler, create a class by implementing the interface System.Configuration.IconfigurationSectionHandler.

A sample class declaration is:

public class MyConfigHandler:IconfigurationSectionHandler{…}

There is only one method defined in the interface IconfigurationSectionHandler with the following signature:

object Create (object parent, object configContext, XmlNode section)
  • parent - The configuration settings in a corresponding parent configuration section.
  • configContext - An HttpConfigurationContext when Create is called from the ASP.NET configuration system. Otherwise, this parameter is reserved and is a null reference.
  • section - The XmlNode that contains the configuration information from the configuration file. Provides direct access to the XML contents of the configuration section.
  • Return value - A configuration object.

Sample SectionHandler class definition follows:

public class MyConfigHandler:IConfigurationSectionHandler
    public MyConfigHandler(){}

    public object Create(object parent, 
           object configContext, System.Xml.XmlNode section)
        CCompanyAddr objCompany=new CCompanyAddr();
        objCompany.CompanyName = section.SelectSingleNode("companyName").InnerText;
        objCompany.DoorNo = section.SelectSingleNode("doorNo").InnerText;
        objCompany.Street = section.SelectSingleNode("street").InnerText;
        objCompany.City = section.SelectSingleNode("city").InnerText;
        objCompany.PostalCode = 
        objCompany.Country = section.SelectSingleNode("country").InnerText;
        return objCompany;

CCompanyAddr is another class defined with the above used properties with corresponding private members.

I have declared a section in the App.config file as follows:

    <sectionGroup name="companyInfo">
    <section name="companyAddress" 


ConfigSections is the name of the namespace.

Then the section is declared in App.config file as follows:

<companyName>Axxonet Solutions India Pvt Ltd</companyName>
<street>13th Cross, Indira Nagar, 2nd Stage</street>

To read companyAddress settings from App.config, we have to use the ConfigurationSettings.GetConfig as follows:

CCompanyAddr obj = 
  • ConfigurationSettings.GetConfig - Returns configuration settings for a user-defined configuration section.


I have written a very simple application to understand these section handlers, so I haven’t commented my code. Feel free to reply.


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

Palanisamy Veerasingam
India India
No Biography provided

Comments and Discussions

GeneralRe: Error: Configuration system failed to initialize Pin
Palanisamy Veerasingam31-May-06 20:37
MemberPalanisamy Veerasingam31-May-06 20:37 
GeneralRe: Error: Configuration system failed to initialize Pin
Jack Rwland1-Jun-06 7:42
MemberJack Rwland1-Jun-06 7:42 
AnswerRe: Error: Configuration system failed to initialize Pin
sahami12-Oct-06 9:52
Membersahami12-Oct-06 9:52 
GeneralRe: Error: Configuration system failed to initialize Pin
rwg23-Jan-07 13:16
professionalrwg23-Jan-07 13:16 
GeneralRe: Error: Configuration system failed to initialize Pin
pkp00211-Jul-07 3:32
Memberpkp00211-Jul-07 3:32 
GeneralRe: Error: Configuration system failed to initialize Pin
Asghar Panahy8-May-07 7:00
MemberAsghar Panahy8-May-07 7:00 
GeneralVery good Pin
ernie_hudds7-Feb-06 0:56
Memberernie_hudds7-Feb-06 0:56 
GeneralIs that possible to have more than company addres Pin
hulinning16-Dec-05 9:53
Memberhulinning16-Dec-05 9:53 
Thanks for your tip.

I wonder If I could create multiple section under Company Address.

For my application, this is what I need to read in. I have multiple section under MachineConfiguration section group.
<name>Machine 1

<name>Machine 2

GeneralNamespace/Assembly Pin
henke_steen3-Nov-05 1:54
Memberhenke_steen3-Nov-05 1:54 
GeneralRe: Namespace/Assembly Pin
ionized54-Nov-05 4:40
Memberionized54-Nov-05 4:40 
GeneralGood article! Pin
Michael van Rooijen21-Oct-05 4:16
MemberMichael van Rooijen21-Oct-05 4:16 
GeneralAppSettings.xml file Pin
Anonymous18-Aug-05 1:18
MemberAnonymous18-Aug-05 1:18 
GeneralRe: AppSettings.xml file Pin
Palanisamy Veerasingam18-Aug-05 19:37
MemberPalanisamy Veerasingam18-Aug-05 19:37 
GeneralRe: AppSettings.xml file Pin
Peter Rilling13-Sep-05 22:13
MemberPeter Rilling13-Sep-05 22:13 
GeneralRe: AppSettings.xml file Pin
VlastaH27-Sep-05 15:02
MemberVlastaH27-Sep-05 15:02 
GeneralRe: AppSettings.xml file Pin
cloudking1196622-Jan-10 0:25
Membercloudking1196622-Jan-10 0:25 
GeneralThanks Pin
WebMaster18-Jul-05 0:43
MemberWebMaster18-Jul-05 0:43 
General&gt;(:0) Pin
Sendilkumar.M14-Jul-05 18:43
MemberSendilkumar.M14-Jul-05 18:43 

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.