ConfigurationSection 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表組態檔中的區段。
public ref class ConfigurationSection abstract : System::Configuration::ConfigurationElement
public abstract class ConfigurationSection : System.Configuration.ConfigurationElement
type ConfigurationSection = class
inherit ConfigurationElement
Public MustInherit Class ConfigurationSection
Inherits ConfigurationElement
- 繼承
- 衍生
範例
下列範例示範如何以程序設計方式實作自定義區段。
如需示範如何使用屬性化模型實作和使用自訂區段的完整範例,請參閱 ConfigurationElement。
// Define a custom section.
// The CustomSection type allows to define a custom section
// programmatically.
public sealed class CustomSection :
ConfigurationSection
{
// The collection (property bag) that contains
// the section properties.
private static ConfigurationPropertyCollection _Properties;
// Internal flag to disable
// property setting.
private static bool _ReadOnly;
// The FileName property.
private static readonly ConfigurationProperty _FileName =
new ConfigurationProperty("fileName",
typeof(string),"default.txt",
ConfigurationPropertyOptions.IsRequired);
// The MaxUsers property.
private static readonly ConfigurationProperty _MaxUsers =
new ConfigurationProperty("maxUsers",
typeof(long), (long)1000,
ConfigurationPropertyOptions.None);
// The MaxIdleTime property.
private static readonly ConfigurationProperty _MaxIdleTime =
new ConfigurationProperty("maxIdleTime",
typeof(TimeSpan), TimeSpan.FromMinutes(5),
ConfigurationPropertyOptions.IsRequired);
// CustomSection constructor.
public CustomSection()
{
// Property initialization
_Properties =
new ConfigurationPropertyCollection();
_Properties.Add(_FileName);
_Properties.Add(_MaxUsers);
_Properties.Add(_MaxIdleTime);
}
// This is a key customization.
// It returns the initialized property bag.
protected override ConfigurationPropertyCollection Properties
{
get
{
return _Properties;
}
}
private new bool IsReadOnly
{
get
{
return _ReadOnly;
}
}
// Use this to disable property setting.
private void ThrowIfReadOnly(string propertyName)
{
if (IsReadOnly)
throw new ConfigurationErrorsException(
"The property " + propertyName + " is read only.");
}
// Customizes the use of CustomSection
// by setting _ReadOnly to false.
// Remember you must use it along with ThrowIfReadOnly.
protected override object GetRuntimeObject()
{
// To enable property setting just assign true to
// the following flag.
_ReadOnly = true;
return base.GetRuntimeObject();
}
[StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;'\"|\\",
MinLength = 1, MaxLength = 60)]
public string FileName
{
get
{
return (string)this["fileName"];
}
set
{
// With this you disable the setting.
// Remember that the _ReadOnly flag must
// be set to true in the GetRuntimeObject.
ThrowIfReadOnly("FileName");
this["fileName"] = value;
}
}
[LongValidator(MinValue = 1, MaxValue = 1000000,
ExcludeRange = false)]
public long MaxUsers
{
get
{
return (long)this["maxUsers"];
}
set
{
this["maxUsers"] = value;
}
}
[TimeSpanValidator(MinValueString = "0:0:30",
MaxValueString = "5:00:0",
ExcludeRange = false)]
public TimeSpan MaxIdleTime
{
get
{
return (TimeSpan)this["maxIdleTime"];
}
set
{
this["maxIdleTime"] = value;
}
}
}
' Define a custom section.
' The CustomSection type allows to define a custom section
' programmatically.
NotInheritable Public Class CustomSection
Inherits ConfigurationSection
' The collection (property bag) that contains
' the section properties.
Private Shared _Properties As ConfigurationPropertyCollection
' Internal flag to disable
' property setting.
Private Shared _ReadOnly As Boolean
' The FileName property.
Private Shared _FileName As New ConfigurationProperty( _
"fileName", GetType(String), _
"default.txt", _
ConfigurationPropertyOptions.IsRequired)
' The MaxUsers property.
Private Shared _MaxUsers As New ConfigurationProperty( _
"maxUsers", GetType(Long), _
CType(1000, Long), _
ConfigurationPropertyOptions.None)
' The MaxIdleTime property.
Private Shared _MaxIdleTime As New ConfigurationProperty( _
"maxIdleTime", GetType(TimeSpan), _
TimeSpan.FromMinutes(5), _
ConfigurationPropertyOptions.IsRequired)
' CustomSection constructor.
Public Sub New()
' Property initialization
_Properties = _
New ConfigurationPropertyCollection()
_Properties.Add(_FileName)
_Properties.Add(_MaxUsers)
_Properties.Add(_MaxIdleTime)
End Sub
' This is a key customization.
' It returns the initialized property bag.
Protected Overrides ReadOnly Property Properties() _
As ConfigurationPropertyCollection
Get
Return _Properties
End Get
End Property
Private Shadows ReadOnly Property IsReadOnly() As Boolean
Get
Return _ReadOnly
End Get
End Property
' Use this to disable property setting.
Private Sub ThrowIfReadOnly(propertyName As String)
If IsReadOnly Then
Throw New ConfigurationErrorsException( _
"The property " + propertyName + " is read only.")
End If
End Sub
' Customizes the use of CustomSection
' by setting _ReadOnly to false.
' Remember you must use it along with ThrowIfReadOnly.
Protected Overrides Function GetRuntimeObject() As Object
' To enable property setting just assign true to
' the following flag.
_ReadOnly = True
Return MyBase.GetRuntimeObject()
End Function 'GetRuntimeObject
<StringValidator( _
InvalidCharacters:=" ~!@#$%^&*()[]{}/;'""|\", _
MinLength:=1, MaxLength:=60)> _
Public Property FileName() As String
Get
Return CStr(Me("fileName"))
End Get
Set(ByVal value As String)
' With this you disable the setting.
' Remember that the _ReadOnly flag must
' be set to true in the GetRuntimeObject.
ThrowIfReadOnly("FileName")
Me("fileName") = value
End Set
End Property
<LongValidator( _
MinValue:=1, MaxValue:=1000000, _
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
<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
下列範例是套用至上一個範例的組態檔摘錄。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="CustomSection" type="Samples.AspNet. CustomSection, CustomConfigurationSection, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" restartOnExternalChanges="true" />
</configSections>
<CustomSection fileName="default.txt" maxUsers="1000" maxIdleTime="00:15:00" />
</configuration>
備註
您可以使用 類別 ConfigurationSection 來實作自訂區段類型。 擴充 類別, ConfigurationSection 以提供自定義處理和以程式設計方式存取自定義組態區段。 如需如何使用自定義組態區段的詳細資訊,請參閱 如何:使用 ConfigurationSection 建立自定義組態區段。
區段會向專案中的專案 configSections
註冊其處理類型。 如需範例,請參閱範例一節中顯示的組態檔摘錄。
注意
在舊版 .NET Framework 中,組態區段處理程式是用來以程式設計方式變更組態設定。 現在,所有預設組態區段都會以擴充類別的 ConfigurationSection 類別表示。
給實施者的注意事項
您可以使用程式設計或宣告式 (屬性化) 編碼模型來建立自訂組態區段:
程序設計模型。 此模型需要針對每個區段屬性建立屬性,以取得或設定其值,並將它新增至基礎 ConfigurationElement 基類的內部屬性包。
宣告式模型。 這個更簡單的模型也稱為屬性化模型,可讓您使用 屬性來定義區段屬性,並使用屬性裝飾它。 這些屬性會指示 ASP.NET 組態系統屬性類型及其預設值。 透過反映取得這項資訊,ASP.NET 組態系統會建立區段屬性物件,並執行必要的初始化。
類別 Configuration 允許以程式設計方式存取編輯組態檔。 您可以存取這些檔案以供讀取或寫入,如下所示:
讀取。 GetSection(String)您可以使用 或 GetSectionGroup(String) 讀取組態資訊。 請注意,讀取的使用者或進程必須具有下列許可權:
目前組態階層層級之組態檔的讀取許可權。
所有父組態檔的讀取許可權。
如果您的應用程式需要只讀存取自己的組態,建議您在 Web 應用程式的情況下使用多載方法,或在GetSection(String)用戶端應用程式的情況下使用 GetSection 方法。
這些方法可讓您存取目前應用程式的快取組態值,其效能優於 Configuration 類別。
注意:如果您使用採用path
參數的靜態GetSection
方法,path
參數必須參考正在執行程式碼的應用程式;否則會忽略 參數,並傳回目前執行之應用程式的組態資訊。
撰寫。 您可以使用其中 Save 一種方法來撰寫組態資訊。 請注意,寫入的使用者或進程必須具有下列許可權:
目前組態階層層級之組態檔和目錄的寫入許可權。
讀取所有組態檔的許可權。
建構函式
ConfigurationSection() |
初始化 ConfigurationSection 類別的新執行個體。 |