Export (0) Print
Expand All

NameValueConfigurationCollection Class

Note: This class is new in the .NET Framework version 2.0.

Contains a collection of NameValueConfigurationElement objects. This class cannot be inherited.

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

'Declaration
Public NotInheritable Class NameValueConfigurationCollection
	Inherits ConfigurationElementCollection
'Usage
Dim instance As NameValueConfigurationCollection

public final class NameValueConfigurationCollection extends ConfigurationElementCollection
public final class NameValueConfigurationCollection extends ConfigurationElementCollection

The NameValueConfigurationCollection class allows you to programmatically access a collection of NameValueConfigurationElement objects.

The following code example demonstrates how to use the NameValueConfigurationCollection type.

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

Namespace Samples.AspNet
  Class UsingNameValueConfigurationCollection
    Public Shared Sub Main()
      Try
        ' Set the path of the config file.
        Dim configPath As String = ""

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

        ' Get the section related object.
        Dim configSection As _
        AnonymousIdentificationSection = _
         CType(config.GetSection("system.web/anonymousIdentification"), _
         AnonymousIdentificationSection)

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

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

        ' Create a NameValueConfigurationCollection object.
        Dim myNameValConfigCollection As NameValueConfigurationCollection = _
        New NameValueConfigurationCollection()

        For Each propertyItem As PropertyInformation In _
         configSection.ElementInformation.Properties
          Dim nameValConfigElement As NameValueConfigurationElement = _
           New NameValueConfigurationElement _
           (propertyItem.Name.ToString(), propertyItem.Value.ToString())
          ' Add a NameValueConfigurationElement
          ' to the collection.
          myNameValConfigCollection.Add(nameValConfigElement)
        Next

        ' Count property.
        Console.WriteLine("Collection Count: {0", _
         myNameValConfigCollection.Count)

        ' Item property.
        Console.WriteLine("Value of property 'enabled': {0", _
         myNameValConfigCollection.Item("enabled").Value)

        ' Display the contents of the collection.
        For Each configItem As NameValueConfigurationElement In _
         myNameValConfigCollection
          Console.WriteLine()
          Console.WriteLine("Configuration Details:")
          Console.WriteLine("Name: {0", configItem.Name)
          Console.WriteLine("Value: {0", configItem.Value)
        Next

        ' Remove method.
        Dim myConfigElement As NameValueConfigurationElement = _
         myNameValConfigCollection.Item("domain")
        myNameValConfigCollection.Remove(myConfigElement)

        ' Clear method.
        myNameValConfigCollection.Clear()

      Catch e As Exception
        ' Unknown error.
        Console.WriteLine(e.ToString())
      End Try

      ' Display and wait.
      Console.ReadLine()
    End Sub
  End Class
End Namespace

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 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft