ConfigurationSection Class

Note: This class is new in the .NET Framework version 2.0.

Represents a section within a configuration file.

Namespace: System.Configuration
Assembly: System.Configuration (in system.configuration.dll)

public abstract class ConfigurationSection : ConfigurationElement
public abstract class ConfigurationSection extends ConfigurationElement
public abstract class ConfigurationSection extends ConfigurationElement

You use the ConfigurationSection to implement a custom section type. Extend the ConfigurationSection class to provide custom handling and programmatic access to custom configuration sections.

A section registers its handling type with an entry in the configSections.

Refer to the configuration file excerpt shown in the example next.


In previous versions of the .NET Framework, configuration section handlers were used to make changes to configuration settings programmatically. Now, all of the default configuration sections are represented by classes that extend the ConfigurationSection class.

Notes to Implementers You can use a programmatic or a declarative (attributed) coding model to create custom configuration sections.

  • Programmatic Model. This model requires that for each section attribute you create a property to get and/or set its value and add it to the internal property bag of the underlying ConfigurationElement base class.

  • Declarative Model. This simpler model, also called attributed model, allows you to define a section attribute by using a property and decorate it with attributes. These attributes instruct the ASP.NET configuration system about the property types and their default values. With this information, obtained through reflection, the ASP.NET configuration system creates for you the section property objects and performs the required initialization.

The Configuration is the class that allows programmatic access for editing configuration files. You can access these files for reading or writing as explained next.
  • Reading. You use GetSection or GetSectionGroup to read configuration information. Note the user or process that reads must have the following permissions:

    • Read permission on the configuration file at the current configuration hierarchy level.

    • Read permissions on all the parent configuration files.

If your application needs read-only access to its own configuration, it is recommended you use the GetSection overloaded methods in case of Web applications. Or the GetSection method in case of client applications. These methods provide access to the cached configuration values for the current application, which has better performance than the Configuration class.

If you use a static GetSection method that takes a path parameter, the path parameter must refer to the application in which the code is running, otherwise the parameter is ignored and configuration information for the currently-running application is returned.

  • Writing. You use one of the Save methods to write configuration information. Note the user or process that writes must have the following permissions:

    • Write permission on the configuration file and directory at the current configuration hierarchy level.

    • Read permissions on all the configuration files.

The following example shows how to implement a custom section programmatically.

Refer to ConfigurationElement for a complete example showing how to implement and use a custom section implemented using the attributed model.

// Define a custom section.
// The CustomSection type alows to define a custom section 
// programmatically.
public sealed class CustomSection : 
    // The collection (property bag) that conatains 
    // the section properties.
    private static ConfigurationPropertyCollection _Properties;
    // Internal flag to disable 
    // property setting.
    private static bool _ReadOnly;

    // The FileName property.
    private static readonly ConfigurationProperty _FileName =
        new ConfigurationProperty("fileName", 

    // The MasUsers property.
    private static readonly ConfigurationProperty _MaxUsers =
        new ConfigurationProperty("maxUsers", 
        typeof(long), (long)1000, 
    // The MaxIdleTime property.
    private static readonly ConfigurationProperty _MaxIdleTime =
        new ConfigurationProperty("maxIdleTime", 
        typeof(TimeSpan), TimeSpan.FromMinutes(5), 

    // CustomSection constructor.
    public CustomSection()
        // Property initialization
        _Properties = 
            new ConfigurationPropertyCollection();


    // This is a key customization. 
    // It returns the initialized property bag.
    protected override ConfigurationPropertyCollection Properties
            return _Properties;

    private new bool IsReadOnly
            return _ReadOnly;

    // Use this to disable property setting.
    private void ThrowIfReadOnly(string propertyName)
        if (IsReadOnly)
            throw new ConfigurationErrorsException(
                "The property " + propertyName + " is read only.");

    // Customizes the use of CustomSection
    // by setting _ReadOnly to false.
    // Remember you must use it along with ThrowIfReadOnly.
    protected override object GetRuntimeObject()
        // To enable property setting just assign true to
        // the following flag.
        _ReadOnly = true;
        return base.GetRuntimeObject();

    [StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{/;'\"|\\",
        MinLength = 1, MaxLength = 60)]
    public string FileName
            return (string)this["fileName"];
            // With this you disable the setting.
            // Renemmber that the _ReadOnly flag must
            // be set to true in the GetRuntimeObject.
            this["fileName"] = value;

    [LongValidator(MinValue = 1, MaxValue = 1000000,
        ExcludeRange = false)]
    public long MaxUsers
            return (long)this["maxUsers"];
            this["maxUsers"] = value;

    [TimeSpanValidator(MinValueString = "0:0:30",
        MaxValueString = "5:00:0",
        ExcludeRange = false)]
    public TimeSpan MaxIdleTime
            return  (TimeSpan)this["maxIdleTime"];
            this["maxIdleTime"] = value;

This is an excerpt of the configuration file as it applies to the previous example.

<?xml version="1.0" encoding="utf-8"?>



<section name="CustomSection" type="Samples.AspNet, CustomConfigurationSection, Version=, Culture=neutral, PublicKeyToken=null" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" restartOnExternalChanges="true" />


<CustomSection fileName="default.txt" maxUsers="1000" maxIdleTime="00:15:00" />


       Derived Classes

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions