ConfigurationSection Class
Represents a section within a configuration file.
Assembly: System.Configuration (in System.Configuration.dll)
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.Configuration.UriSection
System.Data.Services.Configuration.DataServicesFeaturesSection
System.Drawing.Configuration.SystemDrawingSection
System.IdentityModel.Configuration.SystemIdentityModelSection
System.IdentityModel.Services.Configuration.SystemIdentityModelServicesSection
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.Caching.Configuration.MemoryCacheSection
System.Runtime.Serialization.Configuration.DataContractSerializerSection
System.Runtime.Serialization.Configuration.NetDataContractSerializerSection
System.ServiceModel.Activation.Configuration.DiagnosticSection
System.ServiceModel.Activation.Configuration.NetPipeSection
System.ServiceModel.Activation.Configuration.NetTcpSection
System.ServiceModel.Activities.Configuration.WorkflowHostingOptionsSection
System.ServiceModel.Activities.Tracking.Configuration.TrackingSection
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.ProtocolMappingSection
System.ServiceModel.Configuration.ServiceHostingEnvironmentSection
System.ServiceModel.Configuration.ServicesSection
System.ServiceModel.Configuration.StandardEndpointsSection
System.ServiceModel.Routing.Configuration.RoutingSection
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.FullTrustAssembliesSection
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.PartialTrustVisibleAssembliesSection
System.Web.Configuration.ProcessModelSection
System.Web.Configuration.ProfileSection
System.Web.Configuration.ProtocolsSection
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
System.Xml.XmlConfiguration.XmlReaderSection
System.Xml.XmlConfiguration.XsltConfigSection
| Name | Description | |
|---|---|---|
![]() | ConfigurationSection() | Initializes a new instance of the ConfigurationSection class. |
| Name | Description | |
|---|---|---|
![]() | CurrentConfiguration | Gets a reference to the top-level Configuration instance that represents the configuration hierarchy that the current ConfigurationElement instance belongs to.(Inherited from ConfigurationElement.) |
![]() | ElementInformation | Gets an ElementInformation object that contains the non-customizable information and functionality of the ConfigurationElement object. (Inherited from ConfigurationElement.) |
![]() | ElementProperty | Gets the ConfigurationElementProperty object that represents the ConfigurationElement object itself.(Inherited from ConfigurationElement.) |
![]() | EvaluationContext | Gets the ContextInformation object for the ConfigurationElement object.(Inherited from ConfigurationElement.) |
![]() | HasContext | (Inherited from ConfigurationElement.) |
![]() | Item[ConfigurationProperty] | Gets or sets a property or attribute of this configuration element.(Inherited from ConfigurationElement.) |
![]() | Item[String] | Gets or sets a property, attribute, or child element of this configuration element.(Inherited from ConfigurationElement.) |
![]() | LockAllAttributesExcept | Gets the collection of locked attributes.(Inherited from ConfigurationElement.) |
![]() | LockAllElementsExcept | Gets the collection of locked elements.(Inherited from ConfigurationElement.) |
![]() | LockAttributes | Gets the collection of locked attributes (Inherited from ConfigurationElement.) |
![]() | LockElements | Gets the collection of locked elements.(Inherited from ConfigurationElement.) |
![]() | LockItem | Gets or sets a value indicating whether the element is locked.(Inherited from ConfigurationElement.) |
![]() | Properties | Gets the collection of properties.(Inherited from ConfigurationElement.) |
![]() | SectionInformation | Gets a SectionInformation object that contains the non-customizable information and functionality of the ConfigurationSection object. |
| Name | Description | |
|---|---|---|
![]() | DeserializeElement(XmlReader, Boolean) | Reads XML from the configuration file.(Inherited from ConfigurationElement.) |
![]() | DeserializeSection(XmlReader) | This API supports the product infrastructure and is not intended to be used directly from your code. Reads XML from the configuration file. |
![]() | Equals(Object) | Compares the current ConfigurationElement instance to the specified object.(Inherited from ConfigurationElement.) |
![]() | Finalize() | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.) |
![]() | GetHashCode() | Gets a unique value representing the current ConfigurationElement instance.(Inherited from ConfigurationElement.) |
![]() | GetRuntimeObject() | Returns a custom object when overridden in a derived class. |
![]() | GetTransformedAssemblyString(String) | Returns the transformed version of the specified assembly name. (Inherited from ConfigurationElement.) |
![]() | GetTransformedTypeString(String) | Returns the transformed version of the specified type name.(Inherited from ConfigurationElement.) |
![]() | GetType() | |
![]() | Init() | Sets the ConfigurationElement object to its initial state.(Inherited from ConfigurationElement.) |
![]() | InitializeDefault() | Used to initialize a default set of values for the ConfigurationElement object.(Inherited from ConfigurationElement.) |
![]() | IsModified() | Indicates whether this configuration element has been modified since it was last saved or loaded when implemented in a derived class.(Overrides ConfigurationElement.IsModified().) |
![]() | IsReadOnly() | Gets a value indicating whether the ConfigurationElement object is read-only.(Inherited from ConfigurationElement.) |
![]() | ListErrors(IList) | Adds the invalid-property errors in this ConfigurationElement object, and in all subelements, to the passed list.(Inherited from ConfigurationElement.) |
![]() | MemberwiseClone() | |
![]() | OnDeserializeUnrecognizedAttribute(String, String) | Gets a value indicating whether an unknown attribute is encountered during deserialization.(Inherited from ConfigurationElement.) |
![]() | OnDeserializeUnrecognizedElement(String, XmlReader) | Gets a value indicating whether an unknown element is encountered during deserialization.(Inherited from ConfigurationElement.) |
![]() | OnRequiredPropertyNotFound(String) | Throws an exception when a required property is not found.(Inherited from ConfigurationElement.) |
![]() | PostDeserialize() | Called after deserialization.(Inherited from ConfigurationElement.) |
![]() | PreSerialize(XmlWriter) | Called before serialization.(Inherited from ConfigurationElement.) |
![]() | Reset(ConfigurationElement) | Resets the internal state of the ConfigurationElement object, including the locks and the properties collections.(Inherited from ConfigurationElement.) |
![]() | ResetModified() | Resets the value of the IsModified method to false when implemented in a derived class.(Overrides ConfigurationElement.ResetModified().) |
![]() | SerializeElement(XmlWriter, Boolean) | Writes the contents of this configuration element to the configuration file when implemented in a derived class.(Inherited from ConfigurationElement.) |
![]() | SerializeSection(ConfigurationElement, String, ConfigurationSaveMode) | This API supports the product infrastructure and is not intended to be used directly from your code. Creates an XML string containing an unmerged view of the ConfigurationSection object as a single section to write to a file. |
![]() | SerializeToXmlElement(XmlWriter, String) | Writes the outer tags of this configuration element to the configuration file when implemented in a derived class.(Inherited from ConfigurationElement.) |
![]() | SetPropertyValue(ConfigurationProperty, Object, Boolean) | Sets a property to the specified value.(Inherited from ConfigurationElement.) |
![]() | SetReadOnly() | Sets the IsReadOnly property for the ConfigurationElement object and all subelements.(Inherited from ConfigurationElement.) |
![]() | ShouldSerializeElementInTargetVersion(ConfigurationElement, String, FrameworkName) | Indicates whether the specified element should be serialized when the configuration object hierarchy is serialized for the specified target version of the .NET Framework. |
![]() | ShouldSerializePropertyInTargetVersion(ConfigurationProperty, String, FrameworkName, ConfigurationElement) | Indicates whether the specified property should be serialized when the configuration object hierarchy is serialized for the specified target version of the .NET Framework. |
![]() | ShouldSerializeSectionInTargetVersion(FrameworkName) | Indicates whether the current ConfigurationSection instance should be serialized when the configuration object hierarchy is serialized for the specified target version of the .NET Framework. |
![]() | ToString() | Returns a string that represents the current object.(Inherited from Object.) |
![]() | Unmerge(ConfigurationElement, ConfigurationElement, ConfigurationSaveMode) | Modifies the ConfigurationElement object to remove all values that should not be saved. (Inherited from ConfigurationElement.) |
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 information about how use custom configuration sections, 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. |
Notes to Implementers:
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.
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. 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; } } }
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>
Available since 2.0
Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.



