KeyValueConfigurationCollection 类   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
包含 KeyValueConfigurationElement 对象的集合。
public ref class KeyValueConfigurationCollection : System::Configuration::ConfigurationElementCollection[System.Configuration.ConfigurationCollection(typeof(System.Configuration.KeyValueConfigurationElement))]
public class KeyValueConfigurationCollection : System.Configuration.ConfigurationElementCollection[<System.Configuration.ConfigurationCollection(typeof(System.Configuration.KeyValueConfigurationElement))>]
type KeyValueConfigurationCollection = class
    inherit ConfigurationElementCollectionPublic Class KeyValueConfigurationCollection
Inherits ConfigurationElementCollection- 继承
- 属性
示例
下面的代码示例演示如何使用 KeyValueConfigurationCollection 类型。
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();
    }
  }
}
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
注解
此类表示对的集合,其中每个对由一个用作集合键的字符串和一个字符串值组成。 由于第一个元素是键,因此它在整个集合中必须是唯一的。
构造函数
| KeyValueConfigurationCollection() | 初始化 KeyValueConfigurationCollection 类的新实例。 | 
属性
方法
显式接口实现
| ICollection.CopyTo(Array, Int32) | 将 ConfigurationElementCollection 复制到数组。(继承自 ConfigurationElementCollection) | 
扩展方法
| Cast<TResult>(IEnumerable) | 将 IEnumerable 的元素强制转换为指定的类型。 | 
| OfType<TResult>(IEnumerable) | 根据指定类型筛选 IEnumerable 的元素。 | 
| AsParallel(IEnumerable) | 启用查询的并行化。 | 
| AsQueryable(IEnumerable) | 将 IEnumerable 转换为 IQueryable。 |