共用方式為


ProfileSection 類別

定義

這個 ProfileSection 類別提供一種程式化的方式,可以存取並修改設定檔的該 profile 區段。 無法繼承這個類別。

public ref class ProfileSection sealed : System::Configuration::ConfigurationSection
public sealed class ProfileSection : System.Configuration.ConfigurationSection
type ProfileSection = class
    inherit ConfigurationSection
Public NotInheritable Class ProfileSection
Inherits ConfigurationSection
繼承

範例

以下設定檔摘錄展示了如何宣告式地指定類別多個屬性 ProfileSection 的值。

<system.web>
  <profile enabled = "true"
     defaultProvider="AspNetSqlProfileProvider">
    <providers>
      <add  name="AspNetSqlProfileProvider"
        type="System.Web.Profile.SqlProfileProvider"
        connectionStringName="LocalSqlServer"
        applicationName="/"
        description="Stores and retrieves profile data from the
local Microsoft SQL Server database" />
    </providers>
    <properties>
      <add name = "FirstName"/>
      <add name = "LastName"/>
      <add name = "FavoriteURLs" type =
        "System.Collection.Specialized.StringCollection, System"
        serializeAs = "Xml"/>
      <add name = "ShoppingCart" type =
        "MyCommerce.ShoppingCart, MyCommerce"
        serializeAs = "Binary"/>
      <group name = "SiteColors" >
        <add name = "BackGround"/>
        <add name = "SideBar"/>
        <add name = "ForeGroundText"/>
        <add name = "ForeGroundBorders"/>
      </group>
      <group name="Forums">
        <add name = "HasAvatar" type="bool" provider="Forums"/>
        <add name = "LastLogin" type="DateTime" provider="Forums"/>
        <add name = "TotalPosts" type="int" provider="Forums"/>
      </group>
    </properties>
  </profile>
</system.web>

以下程式碼範例說明如何使用該 ProfileSection 型別。

using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Configuration;
using System.Web.Configuration;

namespace Samples.Aspnet.SystemWebConfiguration
{
    // Accesses the System.Web.Configuration.ProfileSection members
    // selected by the user.
    class UsingProfileSection
    {
        public static void Main()
        {
            // Process the System.Web.Configuration.ProfileSectionobject.
            try
            {
                // Get the Web application configuration.
                System.Configuration.Configuration configuration = 
                    System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/aspnet");
                
                // Get the section.
                System.Web.Configuration.ProfileSection profileSection = 
                    (System.Web.Configuration.ProfileSection) 
                    configuration.GetSection("system.web/profile");

// Get the current AutomaticSaveEnabled property value.
Console.WriteLine(
    "Current AutomaticSaveEnabled value: '{0}'", profileSection.AutomaticSaveEnabled);

// Set the AutomaticSaveEnabled property to false.
profileSection.AutomaticSaveEnabled = false;

                

// Get the current DefaultProvider property value.
Console.WriteLine(
    "Current DefaultProvider value: '{0}'", profileSection.DefaultProvider);

// Set the DefaultProvider property to "AspNetSqlProvider".
profileSection.DefaultProvider = "AspNetSqlProvider";

                

// Get the current Inherits property value.
Console.WriteLine(
    "Current Inherits value: '{0}'", profileSection.Inherits);

// Set the Inherits property to
// "CustomProfiles.MyCustomProfile, CustomProfiles.dll".
profileSection.Inherits = "CustomProfiles.MyCustomProfile, CustomProfiles.dll";

                

// Display all current root ProfilePropertySettings.
Console.WriteLine("Current Root ProfilePropertySettings:");
int rootPPSCtr = 0;
foreach (ProfilePropertySettings rootPPS in profileSection.PropertySettings)
{
    Console.WriteLine("  {0}: ProfilePropertySetting '{1}'", ++rootPPSCtr,
        rootPPS.Name);
}

// Get and modify a root ProfilePropertySettings object.
Console.WriteLine(
    "Display and modify 'LastReadDate' ProfilePropertySettings:");
ProfilePropertySettings profilePropertySettings =
    profileSection.PropertySettings["LastReadDate"];

// Get the current ReadOnly property value.
Console.WriteLine(
    "Current ReadOnly value: '{0}'", profilePropertySettings.ReadOnly);

// Set the ReadOnly property to true.
profilePropertySettings.ReadOnly = true;

// Get the current AllowAnonymous property value.
Console.WriteLine(
    "Current AllowAnonymous value: '{0}'", profilePropertySettings.AllowAnonymous);

// Set the AllowAnonymous property to true.
profilePropertySettings.AllowAnonymous = true;

// Get the current SerializeAs property value.
Console.WriteLine(
    "Current SerializeAs value: '{0}'", profilePropertySettings.SerializeAs);

// Set the SerializeAs property to SerializationMode.Binary.
profilePropertySettings.SerializeAs = SerializationMode.Binary;

// Get the current Type property value.
Console.WriteLine(
    "Current Type value: '{0}'", profilePropertySettings.Type);

// Set the Type property to "System.DateTime".
profilePropertySettings.Type = "System.DateTime";

// Get the current DefaultValue property value.
Console.WriteLine(
    "Current DefaultValue value: '{0}'", profilePropertySettings.DefaultValue);

// Set the DefaultValue property to "March 16, 2004".
profilePropertySettings.DefaultValue = "March 16, 2004";

// Get the current ProviderName property value.
Console.WriteLine(
    "Current ProviderName value: '{0}'", profilePropertySettings.Provider);

// Set the ProviderName property to "AspNetSqlRoleProvider".
profilePropertySettings.Provider = "AspNetSqlRoleProvider";

// Get the current Name property value.
Console.WriteLine(
    "Current Name value: '{0}'", profilePropertySettings.Name);

// Set the Name property to "LastAccessDate".
profilePropertySettings.Name = "LastAccessDate";

// Display all current ProfileGroupSettings.
Console.WriteLine("Current ProfileGroupSettings:");
int PGSCtr = 0;
foreach (ProfileGroupSettings propGroups in profileSection.PropertySettings.GroupSettings)
{
    Console.WriteLine("  {0}: ProfileGroupSetting '{1}'", ++PGSCtr,
        propGroups.Name);
    int PPSCtr = 0;
    foreach (ProfilePropertySettings props in propGroups.PropertySettings)
    {
        Console.WriteLine("    {0}: ProfilePropertySetting '{1}'", ++PPSCtr,
            props.Name);
    }
}

// Add a new group.
ProfileGroupSettings newPropGroup = new ProfileGroupSettings("Forum");
profileSection.PropertySettings.GroupSettings.Add(newPropGroup);

// Add a new PropertySettings to the group.
ProfilePropertySettings newProp = new ProfilePropertySettings("AvatarImage");
newProp.Type = "System.String, System.dll";
newPropGroup.PropertySettings.Add(newProp);

// Remove a PropertySettings from the group.
newPropGroup.PropertySettings.Remove("AvatarImage");
newPropGroup.PropertySettings.RemoveAt(0);

// Clear all PropertySettings from the group.
newPropGroup.PropertySettings.Clear();

                

// Display all current Providers.
Console.WriteLine("Current Providers:");
int providerCtr = 0;
foreach (ProviderSettings provider in profileSection.Providers)
{
    Console.WriteLine("  {0}: Provider '{1}' of type '{2}'", ++providerCtr, 
        provider.Name, provider.Type);
}

// Add a new provider.
profileSection.Providers.Add(new ProviderSettings("AspNetSqlProvider", "...SqlProfileProvider"));

                

// Get the current Enabled property value.
Console.WriteLine(
    "Current Enabled value: '{0}'", profileSection.Enabled);

// Set the Enabled property to false.
profileSection.Enabled = false;

                
                // Update if not locked.
                if (!profileSection.SectionInformation.IsLocked)
                {
                    configuration.Save();
                    Console.WriteLine("** Configuration updated.");
                }
                else
                {
                    Console.WriteLine("** Could not update, section is locked.");
                }
            }
            catch (System.ArgumentException e)
            {
                // Unknown error.
                Console.WriteLine(
                    "A invalid argument exception detected in UsingProfileSection Main. Check your");
                Console.WriteLine("command line for errors.");
            }
        }
    } // UsingProfileSection class end.
} // Samples.Aspnet.SystemWebConfiguration namespace end.
Imports System.Collections
Imports System.Collections.Specialized
Imports System.IO
Imports System.Text
Imports System.Text.RegularExpressions
Imports System.Configuration
Imports System.Web.Configuration

Namespace Samples.Aspnet.SystemWebConfiguration
    ' Accesses the System.Web.Configuration.ProfileSection members
    ' selected by the user.
    Class UsingProfileSection
        Public Shared Sub Main()
            ' Process the System.Web.Configuration.ProfileSectionobject.
            Try
                ' Get the Web application configuration.
                Dim configuration As System.Configuration.Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/aspnet")
                
                ' Get the section.
                Dim profileSection As System.Web.Configuration.ProfileSection = CType(configuration.GetSection("system.web/profile"), System.Web.Configuration.ProfileSection)

' Get the current AutomaticSaveEnabled property value.
Console.WriteLine( _
    "Current AutomaticSaveEnabled value: '{0}'", profileSection.AutomaticSaveEnabled)

' Set the AutomaticSaveEnabled property to false.
profileSection.AutomaticSaveEnabled = false

                

' Get the current DefaultProvider property value.
Console.WriteLine( _
    "Current DefaultProvider value: '{0}'", profileSection.DefaultProvider)

' Set the DefaultProvider property to "AspNetSqlProvider".
profileSection.DefaultProvider = "AspNetSqlProvider"

                

' Get the current Inherits property value.
Console.WriteLine( _
    "Current Inherits value: '{0}'", profileSection.Inherits)

' Set the Inherits property to
' "CustomProfiles.MyCustomProfile, CustomProfiles.dll".
profileSection.Inherits = "CustomProfiles.MyCustomProfile, CustomProfiles.dll"

                


' Display all current root ProfilePropertySettings.
Console.WriteLine("Current Root ProfilePropertySettings:")
Dim rootPPSCtr As Integer = 0
For Each rootPPS As ProfilePropertySettings In profileSection.PropertySettings
    Console.WriteLine("  {0}: ProfilePropertySetting '{1}'", ++rootPPSCtr, _
        rootPPS.Name)
Next

' Get and modify a root ProfilePropertySettings object.
Console.WriteLine( _
    "Display and modify 'LastReadDate' ProfilePropertySettings:")
Dim profilePropertySettings As ProfilePropertySettings = _
    profileSection.PropertySettings("LastReadDate")

' Get the current ReadOnly property value.
Console.WriteLine( _
    "Current ReadOnly value: '{0}'", profilePropertySettings.ReadOnly)

' Set the ReadOnly property to true.
profilePropertySettings.ReadOnly = true

' Get the current AllowAnonymous property value.
Console.WriteLine( _
    "Current AllowAnonymous value: '{0}'", profilePropertySettings.AllowAnonymous)

' Set the AllowAnonymous property to true.
profilePropertySettings.AllowAnonymous = true

' Get the current SerializeAs property value.
Console.WriteLine( _
    "Current SerializeAs value: '{0}'", profilePropertySettings.SerializeAs)

' Set the SerializeAs property to SerializationMode.Binary.
profilePropertySettings.SerializeAs = SerializationMode.Binary

' Get the current Type property value.
Console.WriteLine( _
    "Current Type value: '{0}'", profilePropertySettings.Type)

' Set the Type property to "System.DateTime".
profilePropertySettings.Type = "System.DateTime"

' Get the current DefaultValue property value.
Console.WriteLine( _
    "Current DefaultValue value: '{0}'", profilePropertySettings.DefaultValue)

' Set the DefaultValue property to "March 16, 2004".
profilePropertySettings.DefaultValue = "March 16, 2004"

' Get the current ProviderName property value.
            Console.WriteLine( _
                "Current ProviderName value: '{0}'", profilePropertySettings.Provider)

' Set the ProviderName property to "AspNetSqlRoleProvider".
            profilePropertySettings.Provider = "AspNetSqlRoleProvider"

' Get the current Name property value.
Console.WriteLine( _
    "Current Name value: '{0}'", profilePropertySettings.Name)

' Set the Name property to "LastAccessDate".
profilePropertySettings.Name = "LastAccessDate"

' Display all current ProfileGroupSettings.
Console.WriteLine("Current ProfileGroupSettings:")
Dim PGSCtr As Integer = 0
For Each propGroups As ProfileGroupSettings In profileSection.PropertySettings.GroupSettings
                    Console.WriteLine("  {0}: ProfileGroupSettings '{1}'", ++PGSCtr, _
        propGroups.Name)
    Dim PPSCtr As Integer = 0
    For Each props As ProfilePropertySettings In propGroups.PropertySettings
        Console.WriteLine("    {0}: ProfilePropertySetting '{1}'", ++PPSCtr, _
            props.Name)
    Next
Next

' Add a new group.
Dim newPropGroup As ProfileGroupSettings = new ProfileGroupSettings("Forum")
profileSection.PropertySettings.GroupSettings.Add(newPropGroup)

' Add a new PropertySettings to the group.
Dim newProp As ProfilePropertySettings = new ProfilePropertySettings("AvatarImage")
newProp.Type = "System.String, System.dll"
newPropGroup.PropertySettings.Add(newProp)

' Remove a PropertySettings from the group.
newPropGroup.PropertySettings.Remove("AvatarImage")
newPropGroup.PropertySettings.RemoveAt(0)

' Clear all PropertySettings from the group.
newPropGroup.PropertySettings.Clear()

                

Console.WriteLine("Current Providers:")
Dim providerCtr As Integer = 0
For Each provider As ProviderSettings In profileSection.Providers
    Console.WriteLine("  {0}: Provider '{1}' of type '{2}'", ++providerCtr, _
        provider.Name, provider.Type)
Next

' Add a new provider.
profileSection.Providers.Add(new ProviderSettings("AspNetSqlProvider", "...SqlProfileProvider"))

                

' Get the current Enabled property value.
Console.WriteLine( _
    "Current Enabled value: '{0}'", profileSection.Enabled)

' Set the Enabled property to false.
profileSection.Enabled = false

                
                ' Update if not locked.
                If Not profileSection.SectionInformation.IsLocked Then
                    configuration.Save()
                    Console.WriteLine("** Configuration updated.")
                Else
                    Console.WriteLine("** Could not update, section is locked.")
                End If
            Catch e As System.ArgumentException
                ' Unknown error.
                Console.WriteLine( _
                    "A invalid argument exception detected in UsingProfileSection Main. Check your")
                Console.WriteLine("command line for errors.")
            End Try
        End Sub
    End Class
    
End Namespace ' Samples.Aspnet.SystemWebConfiguration

備註

ProfileSection 類別提供一種程式化存取與修改設定檔 profile 區段內容的方式。 設定檔的 profile 部分指定了使用者設定檔的結構。 執行時,ASP.NET 編譯系統會利用該 profile 區段指定的資訊產生一個稱為 ProfileCommon的類別,該類別由 衍生而 ProfileBase來。 ProfileCommon類別定義是基於設定檔區profile段中定義的屬性。 這個類別允許你存取並修改個別設定檔的數值。 每個使用者設定檔都會建立一個這個類別的實例,你可以透過這個 HttpContext.Profile 屬性存取程式碼中的各個設定檔值。 欲了解更多關於 ASP.NET 2.0新增設定檔功能,請參見 ASP.NET 設定檔屬性總覽

建構函式

名稱 Description
ProfileSection()

使用預設設定初始化該類別的新實例 ProfileSection

屬性

名稱 Description
AutomaticSaveEnabled

取得或設定一個值,決定頁面結束時是否自動儲存使用者個人資料資訊的變更。

CurrentConfiguration

會取得代表該Configuration實例所屬配置階層的頂層ConfigurationElement實例參考。

(繼承來源 ConfigurationElement)
DefaultProvider

取得或設定預設設定檔提供者的名稱。

ElementInformation

取得 ElementInformation 一個包含不可自訂資訊與功能的 ConfigurationElement 物件。

(繼承來源 ConfigurationElement)
ElementProperty

取得 ConfigurationElementProperty 代表該 ConfigurationElement 物件本身的物件。

(繼承來源 ConfigurationElement)
Enabled

取得或設定一個值,表示 ASP.NET 設定功能是否啟用。

EvaluationContext

取得 ContextInformation 物件的 ConfigurationElement 物件。

(繼承來源 ConfigurationElement)
HasContext

獲得一個表示該 CurrentConfiguration 性質是否為 null的值。

(繼承來源 ConfigurationElement)
Inherits

取得或設定一個由 ProfileBase衍生出的自訂型別的型別參考。

Item[ConfigurationProperty]

取得或設定此組態元素的屬性或屬性。

(繼承來源 ConfigurationElement)
Item[String]

取得或設定該配置元素的屬性、屬性或子元素。

(繼承來源 ConfigurationElement)
LockAllAttributesExcept

取得鎖定屬性的集合。

(繼承來源 ConfigurationElement)
LockAllElementsExcept

獲得鎖定元素的集合。

(繼承來源 ConfigurationElement)
LockAttributes

取得鎖定屬性的集合。

(繼承來源 ConfigurationElement)
LockElements

獲得鎖定元素的集合。

(繼承來源 ConfigurationElement)
LockItem

取得或設定一個值,表示該元素是否被鎖定。

(繼承來源 ConfigurationElement)
Properties

取得一組房產。

(繼承來源 ConfigurationElement)
PropertySettings

會得到 RootProfilePropertySettingsCollection 一組 ProfilePropertySettings 物品。

Providers

會得到一組 ProviderSettings 物品。

SectionInformation

取得 SectionInformation 一個包含該物件不可自訂資訊與功能的 ConfigurationSection 物件。

(繼承來源 ConfigurationSection)

方法

名稱 Description
DeserializeElement(XmlReader, Boolean)

從設定檔讀取 XML。

(繼承來源 ConfigurationElement)
DeserializeSection(XmlReader)

從設定檔讀取 XML。

(繼承來源 ConfigurationSection)
Equals(Object)

將目前 ConfigurationElement 實例與指定的物件進行比較。

(繼承來源 ConfigurationElement)
GetHashCode()

會獲得代表當前 ConfigurationElement 實例的唯一值。

(繼承來源 ConfigurationElement)
GetRuntimeObject()

在衍生類別中覆寫時,會回傳一個自訂物件。

(繼承來源 ConfigurationSection)
GetTransformedAssemblyString(String)

回傳指定的組裝名稱轉換後版本。

(繼承來源 ConfigurationElement)
GetTransformedTypeString(String)

回傳指定型別名稱的轉換後版本。

(繼承來源 ConfigurationElement)
GetType()

取得目前實例的 Type

(繼承來源 Object)
Init()

將物件設定 ConfigurationElement 為初始狀態。

(繼承來源 ConfigurationElement)
InitializeDefault()

用於初始化物件的預設值 ConfigurationElement 集合。

(繼承來源 ConfigurationElement)
IsModified()

表示此組態元素自最後儲存或載入後是否被修改,且在衍生類別中實作。

(繼承來源 ConfigurationSection)
IsReadOnly()

會取得一個值,表示該物件是否 ConfigurationElement 為唯讀。

(繼承來源 ConfigurationElement)
ListErrors(IList)

將此 ConfigurationElement 物件及所有子元素中的無效屬性錯誤加入已傳遞的清單中。

(繼承來源 ConfigurationElement)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
OnDeserializeUnrecognizedAttribute(String, String)

獲得一個值,表示在反序列化過程中是否遇到未知屬性。

(繼承來源 ConfigurationElement)
OnDeserializeUnrecognizedElement(String, XmlReader)

會獲得一個值,表示在反序列化過程中是否遇到未知元素。

(繼承來源 ConfigurationElement)
OnRequiredPropertyNotFound(String)

當找不到所需的屬性時,會拋出例外。

(繼承來源 ConfigurationElement)
PostDeserialize()

在解序後被呼叫。

(繼承來源 ConfigurationElement)
PreSerialize(XmlWriter)

在序列號之前就被打過。

(繼承來源 ConfigurationElement)
Reset(ConfigurationElement)

重置物件的 ConfigurationElement 內部狀態,包括鎖與屬性集合。

(繼承來源 ConfigurationElement)
ResetModified()

在導出類別實作時,將方法的值IsModified()重置為 。false

(繼承來源 ConfigurationSection)
SerializeElement(XmlWriter, Boolean)

當在衍生類別中實作時,會將此組態元素的內容寫入設定檔。

(繼承來源 ConfigurationElement)
SerializeSection(ConfigurationElement, String, ConfigurationSaveMode)

建立一個包含物件未合併視圖 ConfigurationSection 的 XML 字串,作為一個區段來寫入檔案。

(繼承來源 ConfigurationSection)
SerializeToXmlElement(XmlWriter, String)

當在衍生類別中實作時,會將此組態元素的外部標籤寫入設定檔。

(繼承來源 ConfigurationElement)
SetPropertyValue(ConfigurationProperty, Object, Boolean)

將屬性設定為指定的值。

(繼承來源 ConfigurationElement)
SetReadOnly()

設定 IsReadOnly() 物件及所有子元素的 ConfigurationElement 屬性。

(繼承來源 ConfigurationElement)
ShouldSerializeElementInTargetVersion(ConfigurationElement, String, FrameworkName)

當設定物件階層序列化以符合指定目標版本的 .NET Framework 時,指示該元素是否應該序列化。

(繼承來源 ConfigurationSection)
ShouldSerializePropertyInTargetVersion(ConfigurationProperty, String, FrameworkName, ConfigurationElement)

指示當設定物件階層序列化至指定目標版本的 .NET Framework 時,是否應序列化該屬性。

(繼承來源 ConfigurationSection)
ShouldSerializeSectionInTargetVersion(FrameworkName)

指示當前實例是否 ConfigurationSection 應該序列化,當設定物件階層序列化以符合指定目標版本的 .NET 框架時。

(繼承來源 ConfigurationSection)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)
Unmerge(ConfigurationElement, ConfigurationElement, ConfigurationSaveMode)

修改物件 ConfigurationElement 以移除所有不應儲存的值。

(繼承來源 ConfigurationElement)

適用於

另請參閱