Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
KeyValueConfigurationCollection Class
Collapse the table of content
Expand the table of content

KeyValueConfigurationCollection Class

Contains a collection of KeyValueConfigurationElement objects.

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

public class KeyValueConfigurationCollection : ConfigurationElementCollection

This class represents a collection of pairs, where each pair consists of a string serving as a collection key and a string value. Because the first element is a key, it must be unique across the collection.

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

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Web;
using System.Web.Configuration;

namespace Samples.Aspnet.Config
  class KeyValueConfigCollection
    static void Main(string[] args)
        // Set the path of the config file.
        string configPath = "/aspnet";

        // Get the Web application configuration object.
        Configuration config =

        // Get the section related object.
        AppSettingsSection configSection =

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

        // Display Config details.
        Console.WriteLine("File Path: {0}",
        Console.WriteLine("Section Path: {0}",

        // Create the KeyValueConfigurationElement.
        KeyValueConfigurationElement myAdminKeyVal = 
          new KeyValueConfigurationElement(
          "myAdminTool", "admin.aspx");

        // Determine if the configuration contains 
        // any KeyValueConfigurationElements.
        KeyValueConfigurationCollection configSettings = 
        if (configSettings.AllKeys.Length == 0)
          // Add KeyValueConfigurationElement to collection.

          if (!configSection.SectionInformation.IsLocked)
            Console.WriteLine("** Configuration updated.");
            Console.WriteLine("** Could not update, section is locked.");

        // Get the KeyValueConfigurationCollection  
        // from the configuration.
        KeyValueConfigurationCollection settings = 

        // Display each KeyValueConfigurationElement. 
        foreach (KeyValueConfigurationElement keyValueElement in settings)
          Console.WriteLine("Key: {0}", keyValueElement.Key);
          Console.WriteLine("Value: {0}", keyValueElement.Value);

      catch (Exception e)
        // Unknown error.

      // Display and wait

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

Community Additions

© 2015 Microsoft