Microsoft & .NET.NET.NET Tip: Create a Custom Configuration Section in .NET 2.0

.NET Tip: Create a Custom Configuration Section in .NET 2.0 content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

The configuration file concept in .NET makes it much easier to change runtime settings without having to recompile. These files hearken back to the days of .INI files in previous versions of Windows, long before the dreaded Registry. Although you can keep all your settings in the appSettings section, you also can add your own custom configuration sections to make your settings a bit more orderly and easy to find within a configuration file. This tip shows you how to build a custom configuration section to hold settings dealing with sending e-mail.

The Configuration Class

The first thing you need is a configuration class that derives from the System.Configuration.ConfigurationSection class. This class will have a property for each configuration property you want to set. Here’s the class I created:

public class MailManagerConfiguration : ConfigurationSection
public MailManagerConfiguration()
DefaultValue = “”,
IsRequired = true)]
public String MailServer
{ return (String)this[”serverAddress”]; }
{ this[”serverAddress”] = value; }
[ConfigurationProperty(“messageTemplate”, IsRequired = true)]
public String MessageTemplate
{ return (String)this[”messageTemplate”]; }
{ this[”messageTemplate”] = value; }
[ConfigurationProperty(“baseURL”, IsRequired = true)]
public String BaseURL
{ return (String)this[”baseURL”]; }
{ this[”baseURL”] = value; }
DefaultValue = “Eric Smith <>”,
IsRequired = true)]
public String ReturnAddress
{ return (String)this[”from”]; }
{ this[”from”] = value; }
[ConfigurationProperty(“title”, DefaultValue =
“Announcement from Eric :: {0}”,
IsRequired = true)]
public String TitleFormat
{ return (String)this[”title”]; }
{ this[”title”] = value; }

This class has the following five properties:

  • One for the address of the mail server

  • One for the template for the message

  • A base URL for all the links and images

  • A return address

  • A title format for the message subject

From this example, you can see how these properties can be required or optional and can have a default value in case one is not specified in the configuration file.

The configuration file needs the following code to use this configuration section:

<section name=”MailManagerConfiguration”
MyLibrary” />
from=”Northstar Computer Systems
title=”My Web Site:: {0}” />

The type parameter in the section tag first references the full name of the class (including any namespace), followed by the class name by itself. I generally keep these classes in a separate library for just this reason.

Each of the parameters in the configuration class has a corresponding value in the MailManagerConfiguration tag, as you can see above. The purpose of each value is specific to what the class needs, but you can see how to lay things out in the configuration file.

Instantiate the Configuration Class

The last step is instantiating the configuration class and using the properties, as shown here:

MailManagerConfiguration config =

Once you have this class instantiated, you can read any of the properties, such as config.ReturnAddress, config.TitleFormat, and so on.

By creating this custom configuration section, you’ve made your class more modular and not reliant on the generic appSettings section in the configuration file. Users who work with the class will know what the settings are for and be able to access them more easily in their code.

About the Author

Eric Smith is the owner of Northstar Computer Systems, a Web-hosting company based in Indianapolis, Indiana. He is also a MCT and MCSD who has been developing with .NET since 2001. In addition, he has written or contributed to 12 books covering .NET, ASP, and Visual Basic.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories