NCS Logo - Click for home page Northstar Developer Center
Platforms
All Platforms
.NET Framework (1.x - 4.x)
Active Server Pages
ASP.NET
C#
SQL Server
VB.NET
Visual Basic

Keywords
.NET Data Types
.NET E-mail
.NET Events
.NET Functions
.NET Object Programming
.NET System.Configuration
.NET System.Diagnostics
.NET System.IO
.NET System.Net
.NET System.Net.Sockets
Active Data Objects
ASP Architecture
ASP Black Belt
ASP Built-in Functions
ASP Built-in Objects
ASP Debugging
ASP Performance
ASP Security
ASP Syntax
ASP.NET Authentication
ASP.NET Controls
ASP.NET Data Access
ASP.NET Features
ASP.NET Master Pages
ASP.NET Page Events
ASP.NET Security
ASP.NET ViewState
Atom
Certifications
COM, DCOM, COM+
Data Access
E-Mail
Errors
Exporting Data
HTML Tips
IIS
Object-Oriented Programming
RSS
SQL
Uncategorized ASP Tips
VB API Programming
VB Forms
VB Syntax
XML

Book Support
Visual Basic 6 Bible
ASP Bible
ASP Weekend Crash Course
ASP.NET At Work
Creating Web Services

Create a Custom Configuration Section

Written by Eric Smith, Northstar Computer Systems LLC

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 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()
   {
   }
   [ConfigurationProperty("serverAddress",
                          DefaultValue = "mail.myserver.com",
                          IsRequired = true)]
   public String MailServer
   {
      get
      { return (String)this["serverAddress"]; }
      set
      { this["serverAddress"] = value; }
   }
   [ConfigurationProperty("messageTemplate", IsRequired = true)]
   public String MessageTemplate
   {
      get
      { return (String)this["messageTemplate"]; }
      set
      { this["messageTemplate"] = value; }
   }
   [ConfigurationProperty("baseURL", IsRequired = true)]
   public String BaseURL
   {
      get
      { return (String)this["baseURL"]; }
      set
      { this["baseURL"] = value; }
   }
   [ConfigurationProperty("from",
   DefaultValue = "Eric Smith <eric@myserver.com>",
      IsRequired = true)]
   public String ReturnAddress
   {
      get
      { return (String)this["from"]; }
      set
      { this["from"] = value; }
   }
   [ConfigurationProperty("title", DefaultValue =
                          "Announcement from Eric :: {0}",
                          IsRequired = true)]
   public String TitleFormat
   {
      get
      { return (String)this["title"]; }
      set
      { 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:

<configuration>
   <configSections>
      <section name="MailManagerConfiguration"
               type="MyLibrary.MailManagerConfiguration,
                     MyLibrary" />
   </configSections>
   <MailManagerConfiguration
      serverAddress="mail.myserver.com"
      messageTemplate="C:\Web\mailmanager\template.html"
      baseURL="http://www.myserver.com"
      from="Northstar Computer Systems
         &lt;clientcenter@myserver.com&gt;"
      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.

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

MailManagerConfiguration config =
   (MailManagerConfiguration)ConfigurationManager.GetSection
   ("MailManagerConfiguration");

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.

Keywords: [ .NET System.Configuration ]

Publication Date: 6/30/2006, Last Update: 12/10/2010