ConfigurationSectionGroupCollection Class

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Represents a collection of ConfigurationSectionGroup objects.

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

System.Object
  System.Collections.Specialized.NameObjectCollectionBase
    System.Configuration.ConfigurationSectionGroupCollection

<SerializableAttribute>
Public NotInheritable Class ConfigurationSectionGroupCollection
	Inherits NameObjectCollectionBase

NameDescription
System_CAPS_pubpropertyCount

Gets the number of section groups in the collection.(Overrides NameObjectCollectionBase.Count.)

System_CAPS_pubpropertyItem(Int32)

Gets the ConfigurationSectionGroup object whose index is specified from the collection.

System_CAPS_pubpropertyItem(String)

Gets the ConfigurationSectionGroup object whose name is specified from the collection.

System_CAPS_pubpropertyKeys

Gets the keys to all ConfigurationSectionGroup objects contained in this ConfigurationSectionGroupCollection object.(Overrides NameObjectCollectionBase.Keys.)

NameDescription
System_CAPS_pubmethodAdd(String, ConfigurationSectionGroup)

Adds a ConfigurationSectionGroup object to this ConfigurationSectionGroupCollection object.

System_CAPS_pubmethodClear()

Clears the collection.

System_CAPS_pubmethodCopyTo(ConfigurationSectionGroup(), Int32)

Copies this ConfigurationSectionGroupCollection object to an array.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodGet(Int32)

Gets the specified ConfigurationSectionGroup object contained in the collection.

System_CAPS_pubmethodGet(String)

Gets the specified ConfigurationSectionGroup object from the collection.

System_CAPS_pubmethodGetEnumerator()

Gets an enumerator that can iterate through the ConfigurationSectionGroupCollection object.(Overrides NameObjectCollectionBase.GetEnumerator().)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetKey(Int32)

Gets the key of the specified ConfigurationSectionGroup object contained in this ConfigurationSectionGroupCollection object.

System_CAPS_pubmethodGetObjectData(SerializationInfo, StreamingContext)

Used by the system during serialization.(Overrides NameObjectCollectionBase.GetObjectData(SerializationInfo, StreamingContext).)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodOnDeserialization(Object)

Implements the ISerializable interface and raises the deserialization event when the deserialization is complete.(Inherited from NameObjectCollectionBase.)

System_CAPS_pubmethodRemove(String)

Removes the ConfigurationSectionGroup object whose name is specified from this ConfigurationSectionGroupCollection object.

System_CAPS_pubmethodRemoveAt(Int32)

Removes the ConfigurationSectionGroup object whose index is specified from this ConfigurationSectionGroupCollection object.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubinterfaceSystem_CAPS_privmethodICollection.CopyTo(Array, Int32)

Copies the entire NameObjectCollectionBase to a compatible one-dimensional Array, starting at the specified index of the target array.(Inherited from NameObjectCollectionBase.)

System_CAPS_pubinterfaceSystem_CAPS_privpropertyICollection.IsSynchronized

Gets a value indicating whether access to the NameObjectCollectionBase object is synchronized (thread safe).(Inherited from NameObjectCollectionBase.)

System_CAPS_pubinterfaceSystem_CAPS_privpropertyICollection.SyncRoot

Gets an object that can be used to synchronize access to the NameObjectCollectionBase object.(Inherited from NameObjectCollectionBase.)

NameDescription
System_CAPS_pubmethodAsParallel()

Overloaded. Enables parallelization of a query.(Defined by ParallelEnumerable.)

System_CAPS_pubmethodAsQueryable()

Overloaded. Converts an IEnumerable to an IQueryable.(Defined by Queryable.)

System_CAPS_pubmethodCast(Of TResult)()

Casts the elements of an IEnumerable to the specified type.(Defined by Enumerable.)

System_CAPS_pubmethodOfType(Of TResult)()

Filters the elements of an IEnumerable based on a specified type.(Defined by Enumerable.)

Use the ConfigurationSectionGroupCollection class to iterate through a collection of ConfigurationSectionGroup objects. You can access this collection of objects using the SectionGroups property or the SectionGroups property.

The ConfigurationSectionGroupCollection class is also used in the creation of custom types that extend the ConfigurationSectionGroup class.

The following code example shows how to use the ConfigurationSectionGroupCollection class.

Imports System
Imports System.Configuration
Imports System.Collections


' Define a custom section.
NotInheritable Public Class CustomSection
   Inherits ConfigurationSection


   Public Sub New()
   End Sub 'NewNew

    <ConfigurationProperty("fileName", _
    DefaultValue:="default.txt", _
    IsRequired:=True, _
    IsKey:=False), _
    StringValidator( _
    InvalidCharacters:=" ~!@#$%^&*()[]{}/;'""|\", _
    MinLength:=1, _
    MaxLength:=60)> _
    Public Property FileName() As String
        Get
            Return CStr(Me("fileName"))
        End Get
        Set(ByVal value As String)
            Me("fileName") = value
        End Set
    End Property

    <ConfigurationProperty("maxUsers", _
    DefaultValue:=10000, _
    IsRequired:=False), _
    LongValidator(MinValue:=1, _
    MaxValue:=10000000, _
    ExcludeRange:=False)> _
   Public Property MaxUsers() As Long
        Get
            Return Fix(Me("maxUsers"))
        End Get
        Set(ByVal value As Long)
            Me("maxUsers") = value
        End Set
    End Property

    <ConfigurationProperty("maxIdleTime", _
    DefaultValue:="0:10:0", _
    IsRequired:=False), _
    TimeSpanValidator(MinValueString:="0:0:30", _
    MaxValueString:="5:00:0", _
    ExcludeRange:=False)> _
    Public Property MaxIdleTime() As TimeSpan
        Get
            Return CType(Me("maxIdleTime"), TimeSpan)
        End Get
        Set(ByVal value As TimeSpan)
            Me("maxIdleTime") = value
        End Set
    End Property

End Class 'CustomSection 

' Define a custom section group.

NotInheritable Public Class CustomSectionGroup
   Inherits ConfigurationSectionGroup


   Public Sub New()
   End Sub 'NewNew


   Public ReadOnly Property Custom() As CustomSection
      Get
            Return CType(Sections.Get("CustomSection"), _
            CustomSection)
      End Get
    End Property

End Class 'CustomSectionGroup 


Class UsingCustomSectionGroupCollection


   ' Create a custom section group.
   Shared Sub CreateSectionGroup()
      Try

         Dim customSectionGroup As CustomSectionGroup

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

         ' Create the section group entry  
         ' in the <configSections> and the 
         ' related target section in <configuration>.
         If config.SectionGroups("CustomGroup") Is Nothing Then
            customSectionGroup = New CustomSectionGroup()
            config.SectionGroups.Add("CustomGroup", customSectionGroup)
            customSectionGroup.ForceDeclaration(True)
            config.Save(ConfigurationSaveMode.Full)
         End If

      Catch err As ConfigurationErrorsException
         Console.WriteLine(err.ToString())
      End Try
   End Sub 'CreateSectionGroup


   ' Get the collection group keys i.e.,
   ' the group names.
    'Shared Sub GetAllKeys()

    '   Try
    '         Dim config _
    '         As System.Configuration.Configuration = _
    '         ConfigurationManager.OpenExeConfiguration( _
    '         ConfigurationUserLevel.None)

    '         Dim groups _
    '         As ConfigurationSectionGroupCollection = _
    '         config.SectionGroups

    '      Dim name As String
    '      For Each name In  groups.AllKeys
    '         Console.WriteLine("Key value: {0}", name)
    '      Next name


    '   Catch err As ConfigurationErrorsException
    '      Console.WriteLine(err.ToString())
    '   End Try
    'End Sub 'GetAllKeys

   Shared Sub Clear()

      Try
            Dim config _
            As System.Configuration.Configuration = _
            ConfigurationManager.OpenExeConfiguration( _
            ConfigurationUserLevel.None)


         config.SectionGroups.Clear()

         config.Save(ConfigurationSaveMode.Full)
      Catch err As ConfigurationErrorsException
         Console.WriteLine(err.ToString())
      End Try
   End Sub 'Clear


   Shared Sub GetGroup()

      Try
            Dim config _
            As System.Configuration.Configuration = _
            ConfigurationManager.OpenExeConfiguration( _
            ConfigurationUserLevel.None)

            Dim groups _
            As ConfigurationSectionGroupCollection = _
            config.SectionGroups

            Dim customGroup _
            As ConfigurationSectionGroup = _
            groups.Get("CustomGroup")


         If customGroup Is Nothing Then
                Console.WriteLine( _
                "Failed to load CustomGroup.")
         Else
            ' Display section information
                Console.WriteLine("Name:       {0}", _
                customGroup.Name)
                Console.WriteLine("Type:   {0}", _
                customGroup.Type)
         End If


      Catch err As ConfigurationErrorsException
         Console.WriteLine(err.ToString())
      End Try
   End Sub 'GetGroup


   Shared Sub GetEnumerator()

      Try
            Dim config _
            As System.Configuration.Configuration = _
            ConfigurationManager.OpenExeConfiguration( _
            ConfigurationUserLevel.None)

            Dim groups _
            As ConfigurationSectionGroupCollection = _
            config.SectionGroups

            Dim groupEnum As IEnumerator = _
            groups.GetEnumerator()

         Dim i As Integer = 0
         While groupEnum.MoveNext()
            Dim groupName As String = groups.GetKey(i)
            Console.WriteLine("Group name: {0}", groupName)
            i += 1
         End While

      Catch err As ConfigurationErrorsException
         Console.WriteLine(err.ToString())
      End Try
   End Sub 'GetEnumerator


   ' Get the collection keys i.e., the
   ' group names.
   Shared Sub GetKeys()

      Try
            Dim config _
          As System.Configuration.Configuration = _
          ConfigurationManager.OpenExeConfiguration( _
          ConfigurationUserLevel.None)

            Dim groups _
            As ConfigurationSectionGroupCollection = _
            config.SectionGroups

         Dim key As String
         For Each key In  groups.Keys

            Console.WriteLine("Key value: {0}", key)
         Next key


      Catch err As ConfigurationErrorsException
         Console.WriteLine(err.ToString())
      End Try
   End Sub 'GetKeys


   Shared Sub GetItems()

      Try
            Dim config _
        As System.Configuration.Configuration = _
        ConfigurationManager.OpenExeConfiguration( _
        ConfigurationUserLevel.None)

            Dim groups _
            As ConfigurationSectionGroupCollection = _
            config.SectionGroups

            Dim group1 As ConfigurationSectionGroup = _
            groups.Get("system.net")

            Dim group2 As ConfigurationSectionGroup = _
            groups.Get("system.web")


         Console.WriteLine("Group1: {0}", group1.Name)

         Console.WriteLine("Group2: {0}", group2.Name)

      Catch err As ConfigurationErrorsException
         Console.WriteLine(err.ToString())
      End Try
   End Sub 'GetItems


   Shared Sub Remove()

      Try

            Dim config _
        As System.Configuration.Configuration = _
        ConfigurationManager.OpenExeConfiguration( _
        ConfigurationUserLevel.None)

            Dim groups _
            As ConfigurationSectionGroupCollection = _
            config.SectionGroups

            Dim customGroup _
            As ConfigurationSectionGroup = groups.Get("CustomGroup")

         If Not (customGroup Is Nothing) Then
            config.SectionGroups.Remove("CustomGroup")
            config.Save(ConfigurationSaveMode.Full)
         Else
                Console.WriteLine( _
                "CustomGroup does not exists.")
         End If

      Catch err As ConfigurationErrorsException
         Console.WriteLine(err.ToString())
      End Try
   End Sub 'Remove



   ' Add custom section to the group.
   Shared Sub AddSection()
      Try

         Dim customSection As CustomSection

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

            Dim groups _
            As ConfigurationSectionGroupCollection = _
            config.SectionGroups

         ' Create the section entry  
         ' in the <configSections> and the 
         ' related target section in <configuration>.
         Dim customGroup As ConfigurationSectionGroup
            customGroup = groups.Get("CustomGroup")

            If customGroup.Sections.Get( _
            "CustomSection") Is Nothing Then
                customSection = New CustomSection()
                customGroup.Sections.Add( _
                "CustomSection", customSection)
                customSection.SectionInformation.ForceSave = True
                config.Save(ConfigurationSaveMode.Full)
            End If
      Catch err As ConfigurationErrorsException
         Console.WriteLine(err.ToString())
      End Try
   End Sub 'AddSection


   ' Exercise the collection.
   ' Uncomment the function you want to exercise.
    ' Start with CreateSectionGroup().
    Public Overloads Shared Sub Main(ByVal args() As String)
        CreateSectionGroup()
        AddSection()
        ' GetEnumerator();
        ' GetKeys();
        ' GetItems();
        ' Remove();
        ' Clear();

    End Sub 'Main ' GetAllKeys();
End Class 'UsingCustomSectionGroupCollection ' GetGroup();

The following example is an excerpt of the configuration file used by the previous example.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="CustomSection" 
      type="Samples.AspNet.Configuration.CustomSection, ConfigurationSectionCollection, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" restartOnExternalChanges="true" />
  /configSections>

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

</configuration>

.NET Framework
Available since 2.0

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

Return to top
Show: