Click here to Skip to main content
15,891,951 members
Articles / Web Development / ASP.NET

Creating a Custom Configuration Section in C#

Rate me:
Please Sign up or sign in to vote.
4.79/5 (32 votes)
18 Sep 2007CPOL6 min read 319.3K   4.4K   111  
Creation of a custom configuration section similar to AppSettings. Stores settings for both Development and Production environments, and returns the appropriate settings based on the machine’s configuration in which the application is being executed.
/*********************************************************************
 * Developer     : Dennis Fazekas
 * Email Address : dennis_fazekas@shi.com
 * Date          : September 2007
 * Description   : 
 * 
 * Changes
 * --------------------------------------------------------------------
 * Date       | Developer       | Description
 * --------------------------------------------------------------------
 * 09/04/2007 | Dennis Fazekas  | Added these comments
 * 09/12/2007 | Dennis Fazekas  | Changed Namespace
**********************************************************************/
using System;
using System.Web.Configuration;

namespace SHI.WebTeam.Common.ConfigurationManager
{
   /// <summary>
   /// This class is actually what loads the custom settings.
   /// </summary>
   public class ShiConfiguration : System.Configuration.ConfigurationSection
   {
      private static string sConfigurationSectionConst = "shiConfiguration";

      /// <summary>
      /// Returns an shiConfiguration instance
      /// </summary>
      public static ShiConfiguration GetConfig()
      {

         return (ShiConfiguration)System.Configuration.ConfigurationManager.
            GetSection(ShiConfiguration.sConfigurationSectionConst) ??
            new ShiConfiguration();

      }
      [System.Configuration.ConfigurationProperty("shiSettings")]
      public ShiSettingCollection shiSettings
      {
         get
         {
            return (ShiSettingCollection)this["shiSettings"] ?? 
               new ShiSettingCollection();
         }
      }
   }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions