This documentation is archived and is not being maintained.

KeyValueConfigurationElement Class

Represents a configuration element that contains a key/value pair.

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

'Declaration
Public Class KeyValueConfigurationElement _
	Inherits ConfigurationElement
'Usage
Dim instance As KeyValueConfigurationElement

The KeyValueConfigurationElement object inherits from the ConfigurationElement base class. The ConfigurationElement object represents an element within a configuration file. The KeyValueConfigurationElement object can belong in a KeyValueConfigurationCollection collection.

The following code example demonstrates how to use members of the KeyValueConfigurationElement class.

Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Configuration
Imports System.Web
Imports System.Web.Configuration

Namespace Samples.Aspnet.Config
  Class KeyValueConfigCollection
    Public Shared Sub Main()
      Try 
        ' Set the path of the config file. 
        Dim configPath As String = "/aspnet" 

        ' Get the Web application configuration object. 
        Dim config As Configuration = _
          WebConfigurationManager.OpenWebConfiguration(configPath)

        ' Get the section related object. 
        Dim configSection As System.Configuration.AppSettingsSection = _
        CType(config.GetSection("appSettings"), System.Configuration.AppSettingsSection)

        '      Dim configSection As AppSettingsSection = _ 
        '       (AppSettingsSection)config.GetSection("appSettings") 

        ' Display title and info.
        Console.WriteLine("ASP.NET Configuration Info")
        Console.WriteLine()

        ' Display Config details.
        Console.WriteLine("File Path: {0}", config.FilePath)
        Console.WriteLine("Section Path: {0}", _
          configSection.SectionInformation.Name.ToString())
        Console.WriteLine()

        ' Create the KeyValueConfigurationElement. 
        Dim myAdminKeyVal As KeyValueConfigurationElement = _
          New KeyValueConfigurationElement _
          ("myAdminTool", "admin.aspx")


        ' Determine if the configuration contains 
        ' any KeyValueConfigurationElements. 
        Dim configSettings As KeyValueConfigurationCollection = _
        config.AppSettings.Settings()

        If configSettings.AllKeys.Length = 0 Then 
          ' Add KeyValueConfigurationElement to collection.
          config.AppSettings.Settings.Add(myAdminKeyVal)

          If Not configSection.SectionInformation.IsLocked Then
            config.Save()
            Console.WriteLine("** Configuration updated.")
          Else
            Console.WriteLine("** Could not update, section is locked.")
          End If 
        End If 

        ' Get the KeyValueConfigurationCollection  
        ' from the configuration. 
        Dim settings As KeyValueConfigurationCollection = _
        config.AppSettings.Settings()

        ' Display each KeyValueConfigurationElement. 
        Dim keyValueElement As KeyValueConfigurationElement
        For Each keyValueElement In settings
          Console.WriteLine("Key: {0}", keyValueElement.Key)
          Console.WriteLine("Value: {0}", keyValueElement.Value)
          Console.WriteLine()
        Next 

      Catch e As System.ArgumentException
        ' Unknown error.
        Console.WriteLine(e.ToString())
      End Try 
      ' Display and wait
      Console.ReadLine()
    End Sub 
  End Class 
End Namespace

System.Object
  System.Configuration.ConfigurationElement
    System.Configuration.KeyValueConfigurationElement

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
Show: