ConfigurationSection Class
Collapse the table of content
Expand the table of content

ConfigurationSection Class

Represents a section within a configuration file.

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

public abstract class ConfigurationSection : ConfigurationElement

You use the ConfigurationSection class to implement a custom section type. Extend the ConfigurationSection class to provide custom handling and programmatic access to custom configuration sections. For more information, see How to: Create Custom Configuration Sections Using ConfigurationSection.

A section registers its handling type with an entry in the configSections element. For an example, see the configuration file excerpt shown in the Example section.


In previous versions of the .NET Framework, configuration section handlers were used to make changes to configuration settings programmatically. Now, all 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 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 the attributed model, allows you to define a section attribute by using a property and decorating 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 the section property objects and performs the required initialization.

The Configuration class allows programmatic access for editing configuration files. You can access these files for reading or writing as follows:

  • Reading. You use GetSection or GetSectionGroup to read configuration information. Note that 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 the case of Web applications, or the ConfigurationManager.GetSection method in the 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 that 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.

For a complete example that shows how to implement and use a custom section implemented using the attributed model, see ConfigurationElement.

// Define a custom section. 
// The CustomSection type allows to define a custom section  
// programmatically. 
public sealed class CustomSection : 
    // The collection (property bag) that contains  
    // 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 MaxUsers 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. 
            // Remember 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;


The following example 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. CustomSection, CustomConfigurationSection, Version=, Culture=neutral, PublicKeyToken=null" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" restartOnExternalChanges="true" />


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



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 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

© 2015 Microsoft