This documentation is archived and is not being maintained.

ProfileSection Class

The ProfileSection class provides a way to programmatically access and modify the profile section of a configuration file. This class cannot be inherited.

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

public sealed class ProfileSection : ConfigurationSection

The ProfileSection class provides a way to programmatically access and modify the content of the configuration file profile section. The profile section of the configuration file specifies a schema for user profiles. At run time, the ASP.NET compilation system uses the information specified in the profile section to generate a class called ProfileCommon, which is derived from ProfileBase. The ProfileCommon class definition is based on the properties defined in the profile section of the configuration file. The class allows you to access and modify the values for individual profiles. An instance of this class is created for each user profile, and you can access the individual profile values in your code through the HttpContext.Profile property. For more information about the profile features added to ASP.NET 2.0, see ASP.NET Profile Properties Overview.

The following configuration file excerpt shows how to declaratively specify values for several properties of the ProfileSection class.

  <profile enabled = "true" 
      <add  name="AspNetSqlProfileProvider"
        description="Stores and retrieves profile data from the 
local Microsoft SQL Server database" />
      <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 name="Forums">
        <add name = "HasAvatar" type="bool" provider="Forums"/>
        <add name = "LastLogin" type="DateTime" provider="Forums"/>
        <add name = "TotalPosts" type="int" provider="Forums"/>

The following code example shows how to use the ProfileSection type.

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. 
                // Get the Web application configuration.
                System.Configuration.Configuration configuration = 

                // Get the section.
                System.Web.Configuration.ProfileSection profileSection = 

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

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

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

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

// Get the current Inherits property value.
    "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,

// Get and modify a root ProfilePropertySettings object.
    "Display and modify 'LastReadDate' ProfilePropertySettings:");
ProfilePropertySettings profilePropertySettings =

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

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

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

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

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

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

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

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

// Get the current DefaultValue property value.
    "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.
    "Current ProviderName value: '{0}'", profilePropertySettings.Provider);

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

// Get the current Name property value.
    "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,
    int PPSCtr = 0;
    foreach (ProfilePropertySettings props in propGroups.PropertySettings)
        Console.WriteLine("    {0}: ProfilePropertySetting '{1}'", ++PPSCtr,

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

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

// Remove a PropertySettings from the group.

// Clear all PropertySettings from the group.

// 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.
    "Current Enabled value: '{0}'", profileSection.Enabled);

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

                // Update if not locked. 
                if (! profileSection.SectionInformation.IsLocked)
                    Console.WriteLine("** Configuration updated.");
                    Console.WriteLine("** Could not update, section is locked.");
            catch (System.ArgumentException e)
                // Unknown error.
                    "A invalid argument exception detected in UsingProfileSection Main. Check your");
                Console.WriteLine("command line for errors.");
    } // UsingProfileSection class end.

} // Samples.Aspnet.SystemWebConfiguration namespace end.

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0