ConfigurationSection Class
Represents a section within a configuration file.
Assembly: System.Configuration (in System.Configuration.dll)
You use the ConfigurationSection class to implement a custom section type. Extend the ConfigurationSection class to provide custom handling and programmatic access to custom configuration sections. For more information, see How to: Create Custom Configuration Sections Using ConfigurationSection.
A section registers its handling type with an entry in the configSections element. For an example, see the configuration file excerpt shown in the Example section.
Note: |
|---|
In previous versions of the .NET Framework, configuration section handlers were used to make changes to configuration settings programmatically. Now, all the default configuration sections are represented by classes that extend the ConfigurationSection class. |
You can use a programmatic or a declarative (attributed) coding model to create custom configuration sections:
Programmatic model. This model requires that for each section attribute you create a property to get or set its value and add it to the internal property bag of the underlying ConfigurationElement base class.
Declarative model. This simpler model, also called the attributed model, allows you to define a section attribute by using a property and decorating it with attributes. These attributes instruct the ASP.NET configuration system about the property types and their default values. With this information, obtained through reflection, the ASP.NET configuration system creates the section property objects and performs the required initialization.
The Configuration class allows programmatic access for editing configuration files. You can access these files for reading or writing as follows:
Reading. You use GetSection or GetSectionGroup to read configuration information. Note that the user or process that reads must have the following permissions:
Read permission on the configuration file at the current configuration hierarchy level.
Read permissions on all the parent configuration files.
If your application needs read-only access to its own configuration, it is recommended you use the GetSection overloaded methods in the case of Web applications, or the ConfigurationManager.GetSection method in the case of client applications.
These methods provide access to the cached configuration values for the current application, which has better performance than the Configuration class.
Note:If you use a static GetSection method that takes a path parameter, the path parameter must refer to the application in which the code is running; otherwise, the parameter is ignored and configuration information for the currently-running application is returned.
Writing. You use one of the Save methods to write configuration information. Note that the user or process that writes must have the following permissions:
Write permission on the configuration file and directory at the current configuration hierarchy level.
Read permissions on all the configuration files.
| Topic | Location |
|---|---|
| How to: Create Custom Configuration Sections Using IConfigurationSectionHandler | Configuring ASP .NET Web Applications |
| How to: Create Custom Configuration Sections Using ConfigurationSection | Configuring ASP .NET Web Applications |
| How to: Create Custom Configuration Sections Using IConfigurationSectionHandler | Configuring ASP .NET Web Applications |
| How to: Create Custom Configuration Sections Using ConfigurationSection | Configuring ASP .NET Web Applications |
| How to: Create Custom Configuration Sections Using IConfigurationSectionHandler | |
| How to: Create Custom Configuration Sections Using ConfigurationSection |
The following example shows how to implement a custom section programmatically.
For a complete example that shows how to implement and use a custom section implemented using the attributed model, see ConfigurationElement.
' 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 'New ' 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 'ThrowIfReadOnly ' 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 'CustomSection
The following example is an excerpt of the configuration file as it applies to the previous example.
<?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>
System.Configuration.ConfigurationElement
System.Configuration.ConfigurationSection
System.Configuration.AppSettingsSection
System.Configuration.ClientSettingsSection
System.Configuration.ConnectionStringsSection
System.Configuration.DefaultSection
System.Configuration.IgnoreSection
System.Configuration.ProtectedConfigurationSection
System.Net.Configuration.AuthenticationModulesSection
System.Net.Configuration.ConnectionManagementSection
System.Net.Configuration.DefaultProxySection
System.Net.Configuration.RequestCachingSection
System.Net.Configuration.SettingsSection
System.Net.Configuration.SmtpSection
System.Net.Configuration.WebRequestModulesSection
System.Runtime.Serialization.Configuration.DataContractSerializerSection
System.ServiceModel.Activation.Configuration.DiagnosticSection
System.ServiceModel.Activation.Configuration.NetPipeSection
System.ServiceModel.Activation.Configuration.NetTcpSection
System.ServiceModel.Configuration.BehaviorsSection
System.ServiceModel.Configuration.BindingsSection
System.ServiceModel.Configuration.ClientSection
System.ServiceModel.Configuration.ComContractsSection
System.ServiceModel.Configuration.CommonBehaviorsSection
System.ServiceModel.Configuration.DiagnosticSection
System.ServiceModel.Configuration.ExtensionsSection
System.ServiceModel.Configuration.ServiceHostingEnvironmentSection
System.ServiceModel.Configuration.ServicesSection
System.Transactions.Configuration.DefaultSettingsSection
System.Transactions.Configuration.MachineSettingsSection
System.Web.Configuration.AnonymousIdentificationSection
System.Web.Configuration.AuthenticationSection
System.Web.Configuration.AuthorizationSection
System.Web.Configuration.CacheSection
System.Web.Configuration.ClientTargetSection
System.Web.Configuration.CompilationSection
System.Web.Configuration.CustomErrorsSection
System.Web.Configuration.DeploymentSection
System.Web.Configuration.GlobalizationSection
System.Web.Configuration.HealthMonitoringSection
System.Web.Configuration.HostingEnvironmentSection
System.Web.Configuration.HttpCookiesSection
System.Web.Configuration.HttpHandlersSection
System.Web.Configuration.HttpModulesSection
System.Web.Configuration.HttpRuntimeSection
System.Web.Configuration.IdentitySection
System.Web.Configuration.MachineKeySection
System.Web.Configuration.MembershipSection
System.Web.Configuration.OutputCacheSection
System.Web.Configuration.OutputCacheSettingsSection
System.Web.Configuration.PagesSection
System.Web.Configuration.ProcessModelSection
System.Web.Configuration.ProfileSection
System.Web.Configuration.RoleManagerSection
System.Web.Configuration.ScriptingAuthenticationServiceSection
System.Web.Configuration.ScriptingJsonSerializationSection
System.Web.Configuration.ScriptingProfileServiceSection
System.Web.Configuration.ScriptingRoleServiceSection
System.Web.Configuration.ScriptingScriptResourceHandlerSection
System.Web.Configuration.SecurityPolicySection
System.Web.Configuration.SessionPageStateSection
System.Web.Configuration.SessionStateSection
System.Web.Configuration.SiteMapSection
System.Web.Configuration.SqlCacheDependencySection
System.Web.Configuration.TraceSection
System.Web.Configuration.TrustSection
System.Web.Configuration.UrlMappingsSection
System.Web.Configuration.WebControlsSection
System.Web.Configuration.WebPartsSection
System.Web.Configuration.XhtmlConformanceSection
System.Web.Mobile.DeviceFiltersSection
System.Web.Services.Configuration.WebServicesSection
System.Web.UI.MobileControls.MobileControlsSection
System.Windows.Forms.WindowsFormsSection
System.Workflow.Activities.Configuration.ActiveDirectoryRoleFactoryConfiguration
System.Workflow.Activities.ExternalDataExchangeServiceSection
System.Workflow.Runtime.Configuration.WorkflowRuntimeSection
System.Xml.Serialization.Configuration.DateTimeSerializationSection
System.Xml.Serialization.Configuration.SchemaImporterExtensionsSection
System.Xml.Serialization.Configuration.XmlSerializerSection
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.