.NET Framework Class Library
ConfigurationSectionGroup Class

Represents a group of related sections within a configuration file.

Inheritance Hierarchy
SystemObject
  System.ConfigurationConfigurationSectionGroup
    More...

Namespace:   System.Configuration
Assembly:  System.Configuration (in System.Configuration.dll)
Syntax
Public Class ConfigurationSectionGroup
public class ConfigurationSectionGroup
public ref class ConfigurationSectionGroup
type ConfigurationSectionGroup =  class end

The ConfigurationSectionGroup type exposes the following members.

Constructors
  NameDescription
Public method ConfigurationSectionGroupInitializes a new instance of the ConfigurationSectionGroup class.
Top
Properties
  NameDescription
Public property IsDeclarationRequiredGets a value that indicates whether this ConfigurationSectionGroup object declaration is required.
Public property IsDeclaredGets a value that indicates whether this ConfigurationSectionGroup object is declared.
Public property NameGets the name property of this ConfigurationSectionGroup object.
Public property SectionGroupNameGets the section group name associated with this ConfigurationSectionGroup.
Public property SectionGroupsGets a ConfigurationSectionGroupCollection object that contains all the ConfigurationSectionGroup objects that are children of this ConfigurationSectionGroup object.
Public property SectionsGets a ConfigurationSectionCollection object that contains all of ConfigurationSection objects within this ConfigurationSectionGroup object.
Public property TypeGets or sets the type for this ConfigurationSectionGroup object.
Top
Methods
  NameDescription
Public method Equals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected method FinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method ForceDeclarationForces the declaration for this ConfigurationSectionGroup object.
Public method ForceDeclaration(Boolean)Forces the declaration for this ConfigurationSectionGroup object.
Public method GetHashCodeServes as the default hash function. (Inherited from Object.)
Public method GetTypeGets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected method ShouldSerializeSectionGroupInTargetVersionIndicates whether the current ConfigurationSectionGroup instance should be serialized when the configuration object hierarchy is serialized for the specified target version of the .NET Framework.
Public method ToStringReturns a string that represents the current object. (Inherited from Object.)
Top
Remarks

Settings in configuration files (such as the Web.config file) are organized into sections. Because some sections are related, it is often convenient to group them in a section group. The ConfigurationSectionGroup class represents the sectionGroup XML element that is used to group sections when they are defined in the configSections element of a configuration file. Section groups can be nested (a section group can contain other section groups as well as sections). The following example shows a configSections element that defines three nested section groups:

<configSections>
  <sectionGroup name="system.web.extensions"...>
    <sectionGroup name="scripting" ...>
      <section name="scriptResourceHandler".../>
      <sectionGroup name="webServices"...>
        <section name="jsonSerialization" .../>
        <section name="profileService" ... />        <section name="authenticationService" .../>
        <section name="roleService" .../>
      </sectionGroup>
    </sectionGroup>
  </sectionGroup>
</configSections>

The configuration system loads settings from configuration files into ConfigurationSectionGroup objects. You can use the Sections and SectionGroups properties to access the sections and section groups that are contained in a ConfigurationSectionGroup object.

For more information about how to access information from configuration files, see the ConfigurationManager class.

Examples

The following example shows how to use the ConfigurationSectionGroup class to retrieve configuration settings. The example is a console application that reads configuration settings and writes information about each configuration section group and the sections in it to the console.

The Main method loads the configuration settings into a Configuration object, retrieves the SectionGroups collection from the Configuration object, and then calls the ShowSectionGroupCollectionInfo method to display the section property values.

The ShowSectionGroupCollectionInfo method iterates through the section groups and calls the ShowSectionGroupInfo method for each one.

The ShowSectionGroupInfo method displays the name of the section group, some property values, and the names of the sections that it contains. If the section group contains section groups, this method calls ShowSectionGroupCollectionInfo recursively to display those section groups.

The indentLevel field is used to add spaces to the left side of displayed lines to show logical groupings. All lines are limited to 79 characters of text to avoid line wrapping, which would make it harder to distinguish the logical groupings.

Imports System
Imports System.Collections
Imports System.Configuration

Class UsingConfigurationSectionGroup
   Private Shared indentLevel As Integer = 0

    Public Shared Sub Main(ByVal args() As String)

        ' Get the application configuration file. 
        Dim config As System.Configuration.Configuration = _
            ConfigurationManager.OpenExeConfiguration( _
            ConfigurationUserLevel.None)

        ' Get the collection of the section groups. 
        Dim sectionGroups As ConfigurationSectionGroupCollection = _
            config.SectionGroups

        ' Display the section groups.
        ShowSectionGroupCollectionInfo(sectionGroups)
    End Sub 'Main

    Shared Sub ShowSectionGroupCollectionInfo( _
        ByVal sectionGroups _
        As ConfigurationSectionGroupCollection)

        Dim group As ConfigurationSectionGroup
        For Each group In sectionGroups
            ShowSectionGroupInfo(group)
        Next group
    End Sub 'ShowSectionGroupCollectionInfo

    Shared Sub ShowSectionGroupInfo( _
    ByVal sectionGroup As ConfigurationSectionGroup)
        ' Get the section group name.
        indent("Section Group Name: " + sectionGroup.Name)

        ' Get the fully qualified section group name.
        indent("Section Group Name: " + sectionGroup.SectionGroupName)

        indentLevel += 1

        indent("Type: " + sectionGroup.Type)
        indent("Is Group Required?: " + _
           sectionGroup.IsDeclarationRequired.ToString())
        indent("Is Group Declared?: " + _
            sectionGroup.IsDeclared.ToString())
        indent("Contained Sections:")

        indentLevel += 1
        Dim section As ConfigurationSection
        For Each section In sectionGroup.Sections
            indent("Section Name:" + section.SectionInformation.Name)
        Next section
        indentLevel -= 1

        If (sectionGroup.SectionGroups.Count > 0) Then
            indent("Contained Section Groups:")

            indentLevel += 1
            Dim sectionGroups As ConfigurationSectionGroupCollection = _
                sectionGroup.SectionGroups
            ShowSectionGroupCollectionInfo(sectionGroups)
            indentLevel -= 1
        End If

        indent("")
        indentLevel -= 1

    End Sub 'ShowSectionGroupInfo
    Shared Sub indent(ByVal text As String)
        Dim i As Integer 
        For i = 0 To indentLevel - 1
            Console.Write("  ")
        Next i
        Console.WriteLine(Left(text, 79 - indentLevel * 2))
    End Sub 'getSpacer

End Class 'UsingConfigurationSectionGroup 
using System;
using System.Collections;
using System.Configuration;

namespace Samples.AspNet
{
    class UsingConfigurationSectionGroup
    {
        static int indentLevel = 0;

        static void Main(string[] args)
        {

            // Get the application configuration file.
            System.Configuration.Configuration config =
                ConfigurationManager.OpenExeConfiguration(
                ConfigurationUserLevel.None);

            // Get the collection of the section groups.
            ConfigurationSectionGroupCollection sectionGroups =
                config.SectionGroups;

            // Display the section groups.
            ShowSectionGroupCollectionInfo(sectionGroups);
        }

        static void ShowSectionGroupCollectionInfo(
            ConfigurationSectionGroupCollection sectionGroups)
        {
            foreach (ConfigurationSectionGroup sectionGroup in sectionGroups)
            {
                ShowSectionGroupInfo(sectionGroup);
            }
        }

        static void ShowSectionGroupInfo(
            ConfigurationSectionGroup sectionGroup)
        {
            // Get the section group name.
            indent("Section Group Name: " + sectionGroup.Name);

            // Get the fully qualified group name.
            indent("Section Group Name: " + sectionGroup.SectionGroupName);

            indentLevel++;

            indent("Type: " + sectionGroup.Type);
            indent("Is Group Required?: " + 
                sectionGroup.IsDeclarationRequired);
            indent("Is Group Declared?: " + sectionGroup.IsDeclared);
            indent("Contained Sections:");

            indentLevel++;
            foreach (ConfigurationSection section 
                in sectionGroup.Sections)
            {
                indent("Section Name:" + section.SectionInformation.Name);
            }
            indentLevel--;

            // Display contained section groups if there are any. 
            if (sectionGroup.SectionGroups.Count > 0)
            {
                indent("Contained Section Groups:");

                indentLevel++;
                ConfigurationSectionGroupCollection sectionGroups =
                    sectionGroup.SectionGroups;
                ShowSectionGroupCollectionInfo(sectionGroups);
            }

            Console.WriteLine("");
            indentLevel--;
        }

        static void indent(string text)
        {
            for (int i = 0; i < indentLevel; i++)
            {
                Console.Write("  ");
            }
            Console.WriteLine(text.Substring(0, Math.Min(79 - indentLevel * 2, text.Length)));
        }
    }

}
Version Information

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Platforms

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

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