Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

ConfigurationSectionGroup Class

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

Represents a grouping of related sections within a configuration file.

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

'Declaration
Public Class ConfigurationSectionGroup
'Usage
Dim instance As ConfigurationSectionGroup

public class ConfigurationSectionGroup
public class ConfigurationSectionGroup

Because some configuration sections are related, it is often convenient to group them together within a single section group. The ConfigurationSectionGroup class represents the sectionGroup XML element within a configuration file.

Use the Sections property to access the sections contained in this ConfigurationSectionGroup.

A ConfigurationSectionGroup can also contain other ConfigurationSectionGroup objects, which can be accessed through the SectionGroups property.

The following code example shows how to use ConfigurationSectionGroup.

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
        ShowSectionGroupCollectionInfo(sectionGroups)
    End Sub 'Main
   
   Shared Function getSpacer() As String
      Dim spacer As String = ""
      Dim i As Integer
      For i = 0 To indentLevel - 1
         spacer = spacer + "    "
      Next i
      Return spacer
   End Function 'getSpacer
   
   
    Overloads Shared Sub ForceDeclaration( _
    ByVal sectionGroup As ConfigurationSectionGroup)

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

        sectionGroup.ForceDeclaration()

        config.Save(ConfigurationSaveMode.Full)

        Console.WriteLine( _
        "Forced declaration for the group: {0", _
        sectionGroup.Name)
    End Sub 'ForceDeclaration
   
    Overloads Shared Sub ForceDeclaration( _
    ByVal sectionGroup _
    As ConfigurationSectionGroup, _
    ByVal force As Boolean)
        sectionGroup.ForceDeclaration(force)

        Console.WriteLine( _
        "Forced declaration for the group: {0 is {1", _
        sectionGroup.Name, force.ToString())
    End Sub 'ForceDeclaration
   
    Shared Sub ShowSectionGroupCollectionInfo( _
    ByVal sectionGroups _
    As ConfigurationSectionGroupCollection)
        Dim groupName As String
        For Each groupName In sectionGroups.Keys
            Dim sectionGroup _
            As ConfigurationSectionGroup = _
            CType(sectionGroups(groupName), _
            ConfigurationSectionGroup)
            ShowSectionGroupInfo(sectionGroup)
            If sectionGroup.Name = "system.web" Then
                ForceDeclaration(sectionGroup, True)
            End If
        Next groupName
    End Sub 'ShowSectionGroupCollectionInfo
   
    Shared Sub ShowSectionGroupInfo( _
    ByVal sectionGroup As ConfigurationSectionGroup)
        ' Get the group name including the 
        ' parent group names.
        Console.WriteLine((getSpacer() + _
        "Section Group Name: " + sectionGroup.Name))
        ' Get the group name without including
        ' the parent group names.
        Console.WriteLine((getSpacer() + _
        "Section Group Name: " + _
        sectionGroup.SectionGroupName))
        indentLevel += 1

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

        indentLevel += 1

        Dim sectionCollection _
        As ConfigurationSectionCollection = _
        sectionGroup.Sections
        Dim sectionName As String
        For Each sectionName In sectionCollection.Keys
            Dim section As ConfigurationSection = _
            CType(sectionCollection( _
            sectionName), ConfigurationSection)
            Console.WriteLine((getSpacer() + _
            "Section Name:" + _
            section.SectionInformation.Name))
        Next sectionName
        indentLevel -= 1

        Console.WriteLine((getSpacer() + _
        "Contained Section Groups:"))

        indentLevel += 1

        Dim sectionGroups _
        As ConfigurationSectionGroupCollection = _
        sectionGroup.SectionGroups
        ShowSectionGroupCollectionInfo(sectionGroups)
        indentLevel -= 1
    End Sub 'ShowSectionGroupInfo
End Class 'UsingConfigurationSectionGroup 

System.Object
  System.Configuration.ConfigurationSectionGroup
     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

ADD
Show:
© 2015 Microsoft