This documentation is archived and is not being maintained.

Declaring and Accessing Custom Configuration Sections

.NET Framework 1.1

You can create custom configuration sections for your application to read at run time. You group configuration section information into two main areas in the configuration file: the configuration section declaration area and the configuration section settings area. Put configuration section declarations in the <configSections> element. Create a new configuration section by declaring it in a <section> element inside the <configSections> element. The <section> element has two properties:

  • The name attribute, which is the name of the element that contains the information the section handler reads.
  • The type attribute, which is the name of the class that reads the information.

The syntax of the configuration setting depends on the section handler that is associated with the configuration section. See Configuration Sections Schema for more information.

The following example shows how to declare a custom section that uses the SingleTagSectionHandler class.

        <section name="sampleSection"
                 type="System.Configuration.SingleTagSectionHandler" />

    <sampleSection setting1="Value1" setting2="value two" 
                   setting3="third value" />

Accessing Custom Configuration Sections

You can use the static method System.Configuration.ConfigurationSettings.GetConfig to access configuration settings from an application. The class that implements System.Configuration.IConfigurationSectionHandler computes the settings returned by the GetConfig method. Because the IConfigurationSectionHandler.Create method's return value is an Object type, you must cast the object to the type created by the section handler.

To read the settings from sampleSection, cast the object returned from ConfigurationSettings.GetConfig to an IDictionary object.

The following code shows how to retrieve settings from a configuration file.

Imports System
Imports System.Collections
Imports System.Configuration

Public Class MyConfigurationReader
    Public Sub ReadMySettings() 
        Dim sampleTable As IDictionary 
        Dim value1 As String
        Dim value2 As String
        Dim value3 As String

        sampleTable = CType(ConfigurationSettings.GetConfig("sampleSection"), IDictionary)
        value1 = CType(sampleTable("setting1"), String)
        value2 = CType(sampleTable("setting2"), String)
        value3 = CType(sampleTable("setting3"), String)
    End Sub
End Class
using System;
using System.Collections;
using System.Configuration;

class MyConfigurationReader {
    public void ReadMySettings() {
        IDictionary sampleTable = (IDictionary) 
        string value1 = (string)sampleTable["setting1"];
        string value2 = (string)sampleTable["setting2"];
        string value3 = (string)sampleTable["setting3"];

See Also

Configuration Section Settings | Configuration Sections Schema