ConfigurationSectionGroup 类

定义

表示配置文件中的一组相关节。Represents a group of related sections within a configuration file.

public ref class ConfigurationSectionGroup
public class ConfigurationSectionGroup
type ConfigurationSectionGroup = class
Public Class ConfigurationSectionGroup
继承
ConfigurationSectionGroup
派生

示例

下面的示例演示如何使用 ConfigurationSectionGroup 类来检索配置设置。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.

Main方法将配置设置加载到 Configuration 对象中, SectionGroups 从对象中检索集合, Configuration 然后调用 ShowSectionGroupCollectionInfo 方法以显示节属性值。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.

ShowSectionGroupCollectionInfo方法遍历节组,并 ShowSectionGroupInfo 为每个组调用方法。The ShowSectionGroupCollectionInfo method iterates through the section groups and calls the ShowSectionGroupInfo method for each one.

ShowSectionGroupInfo方法显示节组的名称、某些属性值以及它所包含的节的名称。The ShowSectionGroupInfo method displays the name of the section group, some property values, and the names of the sections that it contains. 如果节组包含节组,则此方法 ShowSectionGroupCollectionInfo 将以递归方式调用以显示这些节组。If the section group contains section groups, this method calls ShowSectionGroupCollectionInfo recursively to display those section groups.

indentLevel 字段用于在显示的行的左侧添加空格,以显示逻辑分组。The indentLevel field is used to add spaces to the left side of displayed lines to show logical groupings. 所有行的文本限制为79个字符,以避免换行,这会使区分逻辑分组更难。All lines are limited to 79 characters of text to avoid line wrapping, which would make it harder to distinguish the logical groupings.

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)));
        }
    }
}
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

    Shared Sub ShowSectionGroupCollectionInfo( _
        ByVal sectionGroups _
        As ConfigurationSectionGroupCollection)

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

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

End Class

注解

配置文件中的设置(如 Web.config 文件)被组织成多个部分。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. ConfigurationSectionGroup类表示 sectionGroup XML 元素,该元素在配置文件的元素中定义节时用于对这些节进行分组 configSectionsThe 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). 下面的示例演示一个 configSections 元素,该元素定义三个嵌套的节组: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>  

配置系统将配置文件中的设置加载到 ConfigurationSectionGroup 对象。The configuration system loads settings from configuration files into ConfigurationSectionGroup objects. 您可以使用 SectionsSectionGroups 属性来访问对象中包含的节和节组 ConfigurationSectionGroupYou can use the Sections and SectionGroups properties to access the sections and section groups that are contained in a ConfigurationSectionGroup object.

有关如何访问配置文件中的信息的详细信息,请参阅 ConfigurationManager 类。For more information about how to access information from configuration files, see the ConfigurationManager class.

构造函数

ConfigurationSectionGroup()

初始化 ConfigurationSectionGroup 类的新实例。Initializes a new instance of the ConfigurationSectionGroup class.

属性

IsDeclarationRequired

获取一个值,指示是否需要声明此 ConfigurationSectionGroup 对象。Gets a value that indicates whether this ConfigurationSectionGroup object declaration is required.

IsDeclared

获取一个值,指示是否已声明此 ConfigurationSectionGroup 对象。Gets a value that indicates whether this ConfigurationSectionGroup object is declared.

Name

获取此 ConfigurationSectionGroup 对象的名称属性。Gets the name property of this ConfigurationSectionGroup object.

SectionGroupName

获取与此 ConfigurationSectionGroup 关联的节组名称。Gets the section group name associated with this ConfigurationSectionGroup.

SectionGroups

获取一个 ConfigurationSectionGroupCollection 对象,其中包含作为此 ConfigurationSectionGroup 对象的子对象的所有 ConfigurationSectionGroup 对象。Gets a ConfigurationSectionGroupCollection object that contains all the ConfigurationSectionGroup objects that are children of this ConfigurationSectionGroup object.

Sections

获取一个 ConfigurationSectionCollection 对象,其中包含此 ConfigurationSection 对象中的所有 ConfigurationSectionGroup 对象。Gets a ConfigurationSectionCollection object that contains all of ConfigurationSection objects within this ConfigurationSectionGroup object.

Type

获取或设置此 ConfigurationSectionGroup 对象的类型。Gets or sets the type for this ConfigurationSectionGroup object.

方法

Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
ForceDeclaration()

强制声明此 ConfigurationSectionGroup 对象。Forces the declaration for this ConfigurationSectionGroup object.

ForceDeclaration(Boolean)

强制声明此 ConfigurationSectionGroup 对象。Forces the declaration for this ConfigurationSectionGroup object.

GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
ShouldSerializeSectionGroupInTargetVersion(FrameworkName)

指示在为指定目标版本的 .NET Framework.NET Framework 序列化配置对象层次结构时,是否应序列化当前的 ConfigurationSectionGroup 实例。Indicates whether the current ConfigurationSectionGroup instance should be serialized when the configuration object hierarchy is serialized for the specified target version of the .NET Framework.NET Framework.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

适用于

另请参阅