Custom Configuration Sections in C# Example

Why Custom configuration Section Needed?

In we can read configuration details from our web.config simply by reading the key value using ConfigurationManager.AppSettings["KeyName"] straightway! then you must be wondering what would be the use of configuration framework ?

In big enterprise business you many need to keep separate version of complex object structure information in some configuration file, writing them all in web config file may be dirty and tedious job.

Separate configuration file helps us to read easily maintainable clean configuration information

Custom Configuration Example

Here we create a small XML file to store some email related information and learn how we can read the file using configuration framework

Namespace Required
using System.Configuration;    
using System.Xml;
using System.Xml.Serialization;
Create Custom config file

Step 1: We create an xml file with name “SystemSettings.xml” and store the information below

<?xml version="1.0" encoding="utf-8" ?>
WebTrainingRoom.Com - Web Software Development Training
<div style='background-color:#eee;padding:15px;text-align:right;'>
Read custom config file in C#

Step 2: Create a Handler inherited from IConfigurationSectionHandler and a class called “SystemSettings” with few properties in it.

Notice, above each property there is a XmlElement, and the XmlElement name is kept same as XML tag in above XML file (ex: [XmlElement("SMTPPort")])

public class SystemSettingsHandler : IConfigurationSectionHandler            
    public object Create(object parent, object configContext, System.Xml.XmlNode section)
    XmlSerializer ser = new XmlSerializer(typeof(SystemSettings));
    return ser.Deserialize(new XmlNodeReader(section));
public class SystemSettings
    public string SmtpServer { get; set; }
    public int SMTPPort { get; set; }
    public string SmtpUserName { get; set; }
    public string SmtpPassword { get; set; }
    public static SystemSettings GetSystemSettings(string sectionPath)
        return ConfigurationManager.GetSection(sectionPath) as SystemSettings;
Add custom section to web.config

Step 3: Now you have to add the custom configuration details in web.config inside configuration tag

<sectionGroup name="CustomSetting">
<section name="SystemSettings" type="MyAssemblyName.Configs.SystemSettingsHandler,MyAssemblyName" />
<SystemSettings configSource="ConfigurationFiles\SystemSettings.xml" />

Step 4: Let's check if configuration details coming properly, here we have one class called "WTRMail", and SystemSettings sSetting = SystemSettings.GetSystemSettings("CustomSetting/SystemSettings"); this will load the information into custom object.

public class WTRMail                
    private MyEmail eMail;
    SystemSettings sSetting = SystemSettings.GetSystemSettings("CustomSetting/SystemSettings");
    public WTRMail()
        eMail = new MyEmail(sSetting.SmtpServer);
        eMail.SmtpUserName = sSetting.SmtpUserName;
        eMail.SmtpUserPassword = sSetting.SmtpPassword;
        eMail.SMTPPort = sSetting.SMTPPort;
        eMail.EmailHeader = sSetting.EmailHeader;
        eMail.EmailFooter = sSetting.EmailFooter;

Enjoy, use the WTRMail class to send email in your application

Custom Configuration in C#
Aspnet MVC Training MVC tutorials, learn model view controllers with c#, develop database driven web application using MVC framework.
Hire .Net Developer
Free Tutorials
ASP.NET MVC Interview Questions Answers
Asp.Net MVC C# Examples | Join Asp.Net MVC Course | Core Tutorial