System.Configuration.KeyValueConfigurationElement 类

方法描述

表示包含键/值对的配置元素。

语法定义(C# System.Configuration.KeyValueConfigurationElement 类 的用法)

public class KeyValueConfigurationElement : ConfigurationElement

构造函数

构造函数名称 构造函数描述
KeyValueConfigurationElement 基于提供的参数初始化 KeyValueConfigurationElement 类的新实例。

成员/方法

方法名称 方法描述
DeserializeElement 读取配置文件中的 XML。 (继承自 ConfigurationElement。)
Equals 将当前的 ConfigurationElement 实例与指定的对象进行比较。 (继承自 ConfigurationElement。)
Finalize 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
GetHashCode 获取一个唯一值,表示当前 ConfigurationElement 实例。 (继承自 ConfigurationElement。)
GetTransformedAssemblyString 返回指定程序集名称的转换版本。 (继承自 ConfigurationElement。)
GetTransformedTypeString 返回指定类型名称的转换版本。 (继承自 ConfigurationElement。)
GetType 获取当前实例的 Type。 (继承自 Object。)
Init 基础结构。将 KeyValueConfigurationElement 对象设置为其初始状态。 (重写 ConfigurationElement.Init()。)
InitializeDefault 用于初始化 ConfigurationElement 对象的默认值集。 (继承自 ConfigurationElement。)
IsModified 指示此配置元素自上次被保存或加载后,直到在派生类中被实现时,是否已经更改。 (继承自 ConfigurationElement。)
IsReadOnly 获取一个值,该值指示 ConfigurationElement 对象是否为只读。 (继承自 ConfigurationElement。)
ListErrors 将此 ConfigurationElement 对象以及所有子元素中无效属性的错误添加到传递的列表中。 (继承自 ConfigurationElement。)
MemberwiseClone 创建当前 Object 的浅表副本。 (继承自 Object。)
OnDeserializeUnrecognizedAttribute 获取一个值,该值指示反序列化过程中是否遇到未知特性。 (继承自 ConfigurationElement。)
OnDeserializeUnrecognizedElement 获取一个值,该值指示反序列化过程中是否遇到未知元素。 (继承自 ConfigurationElement。)
OnRequiredPropertyNotFound 未找到所需属性时引发异常。 (继承自 ConfigurationElement。)
PostDeserialize 反序列化后调用。 (继承自 ConfigurationElement。)
PreSerialize 序列化前调用。 (继承自 ConfigurationElement。)
Reset 重置 ConfigurationElement 对象的内部状态,包括锁和属性集合。 (继承自 ConfigurationElement。)
ResetModified 在派生类中实现时,将 IsModified 方法的值重置为 false。 (继承自 ConfigurationElement。)
SerializeElement 当在派生类中实现后,将此配置元素的内容写入配置文件。 (继承自 ConfigurationElement。)
SerializeToXmlElement 当在派生类中实现后,将此配置元素的外部标记写入配置文件。 (继承自 ConfigurationElement。)
SetPropertyValue 将属性设置为指定值。 (继承自 ConfigurationElement。)
SetReadOnly 设置 ConfigurationElement 对象及所有子元素的 IsReadOnly 属性。 (继承自 ConfigurationElement。)
ToString 返回表示当前对象的字符串。 (继承自 Object。)
Unmerge 修改 ConfigurationElement 对象以移除所有将不被保存的值。 (继承自 ConfigurationElement。)

提示和注释

KeyValueConfigurationElement 对象从 ConfigurationElement 基类继承。 ConfigurationElement 对象表示配置文件中的元素。 KeyValueConfigurationElement 对象可放在 KeyValueConfigurationCollection 集合中。

System.Configuration.KeyValueConfigurationElement 类例子

下面的代码示例演示如何使用 KeyValueConfigurationElement 类的成员。

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

命名空间

namespace: System.Configuration

程序集: System.Configuration(在 System.Configuration.dll 中)

线程安全

此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。