ConfigurationSectionGroup 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示組態檔內相關的區段群組。
public ref class ConfigurationSectionGroup
public class ConfigurationSectionGroup
type ConfigurationSectionGroup = class
Public Class ConfigurationSectionGroup
- 繼承
-
ConfigurationSectionGroup
- 衍生
範例
下列範例示範如何使用 ConfigurationSectionGroup 類別來擷取組態設定。 此範例是主控台應用程式,可讀取組態設定,並將每個組態區段群組及其區段的相關信息寫入主控台。
方法Main
會將組態設定Configuration載入 物件、從 Configuration 物件擷取SectionGroups集合,然後呼叫 ShowSectionGroupCollectionInfo
方法來顯示區段屬性值。
方法 ShowSectionGroupCollectionInfo
會逐一查看區段群組,併為每個區段呼叫 ShowSectionGroupInfo
方法。
方法 ShowSectionGroupInfo
會顯示區段群組的名稱、部分屬性值及其包含之區段的名稱。 如果區段群組包含區段群組,這個方法會以遞歸方式呼叫 ShowSectionGroupCollectionInfo
以顯示這些區段群組。
欄位 indentLevel
是用來將空格新增至顯示行左側,以顯示邏輯群組。 所有行限制為 79 個字元的文字,以避免換行,這會使邏輯群組更難區分。
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 檔案) 會組織成區段。 因為某些區段相關,所以在區段群組中分組它們通常很方便。 類別 ConfigurationSectionGroup 代表 sectionGroup
在組態檔的 元素中 configSections
定義區段時,用來分組區段的 XML 專案。 區段群組可以是巢狀群組, (區段群組可以包含其他區段群組,以及區段) 。 下列範例顯示定義三個 configSections
巢狀區段群組的 元素:
<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 設定載入物件。 您可以使用 Sections 和 SectionGroups 屬性來存取 物件中包含的 ConfigurationSectionGroup 區段和區段群組。
如需如何從組態檔存取資訊的詳細資訊,請參閱 類別 ConfigurationManager 。
建構函式
ConfigurationSectionGroup() |
初始化 ConfigurationSectionGroup 類別的新執行個體。 |
屬性
IsDeclarationRequired |
取得值,指出是否需要這個 ConfigurationSectionGroup 物件宣告。 |
IsDeclared |
取得值,指出這個 ConfigurationSectionGroup 物件是否已宣告。 |
Name |
取得這個 ConfigurationSectionGroup 物件的名稱屬性。 |
SectionGroupName |
取得與這個 ConfigurationSectionGroup 相關聯的區段群組名稱。 |
SectionGroups |
取得 ConfigurationSectionGroupCollection 物件,其包含的所有 ConfigurationSectionGroup 物件都是這個 ConfigurationSectionGroup 物件的子系。 |
Sections |
取得 ConfigurationSectionCollection 物件,其包含這個 ConfigurationSection 物件內的所有 ConfigurationSectionGroup 物件。 |
Type |
取得或設定這個 ConfigurationSectionGroup 物件的型別。 |
方法
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
ForceDeclaration() |
強制為此 ConfigurationSectionGroup 物件進行宣告。 |
ForceDeclaration(Boolean) |
強制為此 ConfigurationSectionGroup 物件進行宣告。 |
GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
MemberwiseClone() |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
ShouldSerializeSectionGroupInTargetVersion(FrameworkName) |
指出當組態物件階層針對指定的目標版本串行化 .NET Framework 時,是否應該串行化目前的ConfigurationSectionGroup實例。 |
ToString() |
傳回代表目前物件的字串。 (繼承來源 Object) |