System.Configuration.NameValueConfigurationCollection 类

方法描述

包含 NameValueConfigurationElement 对象的集合。 此类不能被继承。

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

[ConfigurationCollectionAttribute(typeof(NameValueConfigurationElement))]
public sealed class NameValueConfigurationCollection : ConfigurationElementCollection

构造函数

构造函数名称 构造函数描述
NameValueConfigurationCollection 初始化 NameValueConfigurationCollection 类的新实例。

成员/方法

方法名称 方法描述
Add 将 NameValueConfigurationElement 对象添加到集合中。
BaseAdd(ConfigurationElement) 向 ConfigurationElementCollection 添加配置元素。 (继承自 ConfigurationElementCollection。)
BaseAdd(ConfigurationElement, Boolean) 向配置元素集合添加配置元素。 (继承自 ConfigurationElementCollection。)
BaseAdd(Int32, ConfigurationElement) 向配置元素集合添加配置元素。 (继承自 ConfigurationElementCollection。)
BaseClear 从集合中移除所有配置元素对象。 (继承自 ConfigurationElementCollection。)
BaseGet(Int32) 获取位于指定索引位置的配置元素。 (继承自 ConfigurationElementCollection。)
BaseGet(Object) 返回具有指定键的配置元素。 (继承自 ConfigurationElementCollection。)
BaseGetAllKeys 返回 ConfigurationElementCollection 中包含的所有配置元素的键数组。 (继承自 ConfigurationElementCollection。)
BaseGetKey 获取位于指定索引位置的 ConfigurationElement 的键。 (继承自 ConfigurationElementCollection。)
BaseIndexOf 指定 ConfigurationElement 的索引。 (继承自 ConfigurationElementCollection。)
BaseIsRemoved 获取一个值,该值指示是否已从 ConfigurationElementCollection 中移除具有指定键的 ConfigurationElement。 (继承自 ConfigurationElementCollection。)
BaseRemove 从集合中移除 ConfigurationElement。 (继承自 ConfigurationElementCollection。)
BaseRemoveAt 移除位于指定索引位置的 ConfigurationElement。 (继承自 ConfigurationElementCollection。)
Clear 清除 NameValueConfigurationCollection。
CopyTo 将 ConfigurationElementCollection 的内容复制到数组。 (继承自 ConfigurationElementCollection。)
CreateNewElement() 当在派生的类中重写时,创建一个新的 ConfigurationElement。 (继承自 ConfigurationElementCollection。)
CreateNewElement(String) 在派生的类中重写时,创建新的 ConfigurationElement。 (继承自 ConfigurationElementCollection。)
DeserializeElement 读取配置文件中的 XML。 (继承自 ConfigurationElement。)
Equals 将 ConfigurationElementCollection 与指定的对象进行比较。 (继承自 ConfigurationElementCollection。)
Finalize 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。)
GetElementKey 在派生类中重写时获取指定配置元素的元素键。 (继承自 ConfigurationElementCollection。)
GetEnumerator 基础结构。获取用于循环访问 ConfigurationElementCollection 的 IEnumerator。 (继承自 ConfigurationElementCollection。)
GetHashCode 获取表示 ConfigurationElementCollection 实例的唯一值。 (继承自 ConfigurationElementCollection。)
GetTransformedAssemblyString 返回指定程序集名称的转换版本。 (继承自 ConfigurationElement。)
GetTransformedTypeString 返回指定类型名称的转换版本。 (继承自 ConfigurationElement。)
GetType 获取当前实例的 Type。 (继承自 Object。)
Init 将 ConfigurationElement 对象设置为其初始状态。 (继承自 ConfigurationElement。)
InitializeDefault 用于初始化 ConfigurationElement 对象的默认值集。 (继承自 ConfigurationElement。)
IsElementName 指示指定的 ConfigurationElement 是否存在于 ConfigurationElementCollection 中。 (继承自 ConfigurationElementCollection。)
IsElementRemovable 获取一个值,该值指示是否可从 ConfigurationElementCollection 中移除指定的 ConfigurationElement。 (继承自 ConfigurationElementCollection。)
IsModified 在派生的类中重写时,指示从最后一次保存或加载此 ConfigurationElementCollection 后是否对其进行了修改。 (继承自 ConfigurationElementCollection。)
IsReadOnly 获取一个值,该值指示 ConfigurationElementCollection 对象是否为只读。 (继承自 ConfigurationElementCollection。)
ListErrors 将此 ConfigurationElement 对象以及所有子元素中无效属性的错误添加到传递的列表中。 (继承自 ConfigurationElement。)
MemberwiseClone 创建当前 Object 的浅表副本。 (继承自 Object。)
OnDeserializeUnrecognizedAttribute 获取一个值,该值指示反序列化过程中是否遇到未知特性。 (继承自 ConfigurationElement。)
OnDeserializeUnrecognizedElement 导致配置系统引发异常。 (继承自 ConfigurationElementCollection。)
OnRequiredPropertyNotFound 未找到所需属性时引发异常。 (继承自 ConfigurationElement。)
PostDeserialize 反序列化后调用。 (继承自 ConfigurationElement。)
PreSerialize 序列化前调用。 (继承自 ConfigurationElement。)
Remove(NameValueConfigurationElement) 基于所提供的参数,从集合中移除 NameValueConfigurationElement 对象。
Remove(String) 基于所提供的参数,从集合中移除 NameValueConfigurationElement 对象。
Reset 在派生的类中重写时,将 ConfigurationElementCollection 重置为其未被修改时的状态。 (继承自 ConfigurationElementCollection。)
ResetModified 在派生的类中重写时,将 IsModified 属性的值重置为 false。 (继承自 ConfigurationElementCollection。)
SerializeElement 在派生的类中重写时,将配置数据写入配置文件中的 XML 元素。 (继承自 ConfigurationElementCollection。)
SerializeToXmlElement 当在派生类中实现后,将此配置元素的外部标记写入配置文件。 (继承自 ConfigurationElement。)
SetPropertyValue 将属性设置为指定值。 (继承自 ConfigurationElement。)
SetReadOnly 基础结构。为 ConfigurationElementCollection 对象和所有子元素设置 IsReadOnly 属性。 (继承自 ConfigurationElementCollection。)
ToString 返回表示当前对象的字符串。 (继承自 Object。)
Unmerge 基础结构。反转将配置层次结构的不同级别配置的信息合并在一起的效果 (继承自 ConfigurationElementCollection。)

提示和注释

NameValueConfigurationCollection 类可用于以编程方式访问 NameValueConfigurationElement 对象集合。

System.Configuration.NameValueConfigurationCollection 类例子

下面的代码示例演示如何使用 NameValueConfigurationCollection 类型。

#region Using directives

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

#endregion

namespace Samples.AspNet
{
    class UsingNameValueConfigurationCollection
    {
        static void Main(string[] args)
        {
            try
            {
                // Set the path of the config file.
                // Make sure that you have a Web site on the
                // same server called TestConfig.
                string configPath = "/TestConfig";

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

                // Get the section related object.
                AnonymousIdentificationSection configSection =
                  (AnonymousIdentificationSection)config.GetSection
                  ("system.web/anonymousIdentification");

                // 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.
                NameValueConfigurationCollection myNameValConfigCollection =
                  new NameValueConfigurationCollection();

                foreach (PropertyInformation propertyItem in
                  configSection.ElementInformation.Properties)
                {
                    // Assign  domain name.
                    if (propertyItem.Name == "domain")
                        propertyItem.Value = "MyDomain";

                    if (propertyItem.Value != null)
                    {
                        // Enable SSL for cookie exchange.
                        if (propertyItem.Name == "cookieRequireSSL")
                            propertyItem.Value = true;

                        NameValueConfigurationElement nameValConfigElement =
                            new NameValueConfigurationElement
                                (propertyItem.Name.ToString(), propertyItem.Value.ToString());

                        // Add a NameValueConfigurationElement
                        // to the collection.
                        myNameValConfigCollection.Add(nameValConfigElement);

                    }
                }

                // Count property.
                Console.WriteLine("Collection Count: {0}",
                 myNameValConfigCollection.Count);

                // Item property.
                Console.WriteLine("Value of property 'enabled': {0}",
                 myNameValConfigCollection["enabled"].Value);

                // Display the contents of the collection.
                foreach (NameValueConfigurationElement configItem
                  in myNameValConfigCollection)
                {
                    Console.WriteLine();
                    Console.WriteLine("Configuration Details:");
                    Console.WriteLine("Name: {0}", configItem.Name);
                    Console.WriteLine("Value: {0}", configItem.Value);
                }

                // Assign the domain calue.
                configSection.Domain = myNameValConfigCollection["domain"].Value;
                // Assign the SSL required value.
                if (myNameValConfigCollection["cookieRequireSSL"].Value == "true")
                    configSection.CookieRequireSSL = true;

                // Remove domain from the collection.
                NameValueConfigurationElement myConfigElement =
                    myNameValConfigCollection["domain"];
                // Remove method.
                myNameValConfigCollection.Remove(myConfigElement);

                // Save changes to the configuration file.
                // This modifies the Web.config of the TestConfig site.
                config.Save(ConfigurationSaveMode.Minimal, true);

                // Clear the collection.
                myNameValConfigCollection.Clear();
            }

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

            // Display and wait.
            Console.ReadLine();
        }
    }
}

继承层次结构

System.Object

System.Configuration.ConfigurationElement

System.Configuration.ConfigurationElementCollection

System.Configuration.NameValueConfigurationCollection

命名空间

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 系统要求。