Export (0) Print
Expand All

KeyValueConfigurationElement Class

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

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

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

public class KeyValueConfigurationElement : ConfigurationElement
public class KeyValueConfigurationElement extends ConfigurationElement
public class KeyValueConfigurationElement extends ConfigurationElement

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.

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)
    {
      try
      {
        // Set the path of the config file.
        string configPath = "/aspnet";

        // Get the Web application configuration object.
        Configuration config =
          WebConfigurationManager.OpenWebConfiguration(configPath);

        // Get the section related object.
        AppSettingsSection configSection =
          (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.
        KeyValueConfigurationElement myAdminKeyVal = 
          new KeyValueConfigurationElement(
          "myAdminTool", "admin.aspx");

        // Determine if the configuration contains
        // any KeyValueConfigurationElements.
        KeyValueConfigurationCollection configSettings = 
          config.AppSettings.Settings;
        if (configSettings.AllKeys.Length == 0)
        {
          // Add KeyValueConfigurationElement to collection.
          config.AppSettings.Settings.Add(myAdminKeyVal);

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

        // Get the KeyValueConfigurationCollection 
        // from the configuration.
        KeyValueConfigurationCollection settings = 
          config.AppSettings.Settings;

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

      
      catch (Exception e)
      {
        // Unknown error.
        Console.WriteLine(e.ToString());
      

      // Display and wait
      Console.ReadLine();
    
  


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