This documentation is archived and is not being maintained.

ConfigurationPropertyAttribute Class

Declaratively instructs the .NET Framework to instantiate a configuration property. This class cannot be inherited.

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

<AttributeUsageAttribute(AttributeTargets.Property)> _
Public NotInheritable Class ConfigurationPropertyAttribute _
	Inherits Attribute
Dim instance As ConfigurationPropertyAttribute

You use the ConfigurationPropertyAttribute to decorate a configuration property, which will instruct the .NET Framework to instantiate and to initialize the property using the value of the decorating parameter.


The simplest way to create a custom configuration element is to use the attributed (declarative) model. You declare the custom public properties and decorate them with the ConfigurationPropertyAttribute attribute. For each property marked with this attribute, the .NET Framework uses reflection to read the decorating parameters and create a related ConfigurationProperty instance. You can also use the programmatic model, in which case it is your responsibility to declare the custom public properties and return their collection.

The .NET Framework configuration system provides attribute types that you can use during the creation of custom configuration elements. There are two kinds of attribute types:

  1. The types instructing the .NET Framework how to instantiate the custom configuration-element properties. These types include:

  2. The types instructing the .NET Framework how to validate the custom configuration-element properties. These types include:

The following example shows how to define the properties of a custom ConfigurationSection object using the ConfigurationPropertyAttribute attribute.

The example contains two classes. The UrlsSection custom class uses the ConfigurationPropertyAttribute to define its own properties. The UsingConfigurationPropertyAttribute class uses the UrlsSection to read and write the custom section in the application configuration file.

Imports Microsoft.VisualBasic
Imports System
Imports System.Configuration

' Define a custom section. 
' This class shows how to use the ConfigurationPropertyAttribute. 
Public Class UrlsSection
    Inherits ConfigurationSection
    <ConfigurationProperty("name", DefaultValue:="Contoso", IsRequired:=True, IsKey:=True)>
    Public Property Name() As String 
            Return CStr(Me("name"))
        End Get 
        Set(ByVal value As String)
            Me("name") = value
        End Set 
    End Property

    <ConfigurationProperty("url", DefaultValue:="", IsRequired:=True), RegexStringValidator("\w+:\/\/[\w.]+\S*")>
    Public Property Url() As String 
            Return CStr(Me("url"))
        End Get 
        Set(ByVal value As String)
            Me("url") = value
        End Set 
    End Property

    <ConfigurationProperty("port", DefaultValue:=0, IsRequired:=False), IntegerValidator(MinValue:=0, MaxValue:=8080, ExcludeRange:=False)>
    Public Property Port() As Integer 
            Return CInt(Fix(Me("port")))
        End Get 
        Set(ByVal value As Integer)
            Me("port") = value
        End Set 
    End Property 
End Class
Imports System
Imports System.Configuration

Public Class UsingConfigurationPropertyAttribute

    ' Create a custom section and save it in the  
    ' application configuration file. 
    Private Shared Sub CreateCustomSection()

            ' Create a custom configuration section. 
            Dim customSection As New UrlsSection()

            ' Get the current configuration file. 
            Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)

            ' Add the custom section to the application 
            ' configuration file. 
            If config.Sections("CustomSection") Is Nothing Then
                config.Sections.Add("CustomSection", customSection)
            End If 

            ' Save the application configuration file.
            customSection.SectionInformation.ForceSave = True

            Console.WriteLine("Created custom section in the application configuration file: {0}", config.FilePath)

        Catch err As ConfigurationErrorsException
            Console.WriteLine("CreateCustomSection: {0}", err.ToString())
        End Try 

    End Sub 

    Private Shared Sub ReadCustomSection()
            ' Get the application configuration file. 
            Dim config As System.Configuration.Configuration = TryCast(ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None), Configuration)

            ' Read and display the custom section. 
            Dim customSection As UrlsSection = TryCast(config.GetSection("CustomSection"), UrlsSection)
            Console.WriteLine("Reading custom section from the configuration file.")
            Console.WriteLine("Section name: {0}", customSection.Name)
            Console.WriteLine("Url: {0}", customSection.Url)
            Console.WriteLine("Port: {0}", customSection.Port)
        Catch err As ConfigurationErrorsException
            Console.WriteLine("ReadCustomSection(string): {0}", err.ToString())
        End Try 

    End Sub 

    Shared Sub Main(ByVal args() As String)

        ' Get the name of the application. 
        Dim appName As String = Environment.GetCommandLineArgs()(0)

        ' Create a custom section and save it in the  
        ' application configuration file.

        ' Read the custom section saved in the 
        ' application configuration file.

        Console.WriteLine("Press enter to exit.")

    End Sub 
End Class

The following is an excerpt of the configuration file containing the custom section as defined in the previous sample.

<?xml version="1.0" encoding="utf-8"?>
        <section name="CustomSection" type="UrlsSection, UsingConfigurationPropertyAttribute, Version=, Culture=neutral, PublicKeyToken=null" />
    <CustomSection name="Contoso" url="" />


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