Export (0) Print
Expand All
2 out of 19 rated this helpful - Rate this topic

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.

NoteNote

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.
NoteNote

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 : 
    ConfigurationSection
{
    // 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", 
        typeof(string),"default.txt", 
        ConfigurationPropertyOptions.IsRequired);

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

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

        _Properties.Add(_FileName);
        _Properties.Add(_MaxUsers);
        _Properties.Add(_MaxIdleTime);
   

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


    private new bool IsReadOnly
    {
        get
        {
            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
    {
        get
        {
            return (string)this["fileName"];
        
        set
        {
            // With this you disable the setting.
            // Renemmber that the _ReadOnly flag must
            // be set to true in the GetRuntimeObject.
            ThrowIfReadOnly("FileName");
            this["fileName"] = value;
        
    

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

    [TimeSpanValidator(MinValueString = "0:0:30",
        MaxValueString = "5:00:0",
        ExcludeRange = false)]
    public TimeSpan MaxIdleTime
    {
        get
        {
            return  (TimeSpan)this["maxIdleTime"];
        
        set
        {
            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"?>

<configuration>

<configSections>

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

</configSections>

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

</configuration>

System.Object
   System.Configuration.ConfigurationElement
    System.Configuration.ConfigurationSection
       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
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.