KeyValueConfigurationElement Klasa

Definicja

Reprezentuje element konfiguracji, który zawiera parę klucz/wartość.

public ref class KeyValueConfigurationElement : System::Configuration::ConfigurationElement
public class KeyValueConfigurationElement : System.Configuration.ConfigurationElement
type KeyValueConfigurationElement = class
    inherit ConfigurationElement
Public Class KeyValueConfigurationElement
Inherits ConfigurationElement
Dziedziczenie
KeyValueConfigurationElement

Przykłady

W poniższym przykładzie kodu pokazano, jak używać składowych KeyValueConfigurationElement klasy .

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

Uwagi

Obiekt KeyValueConfigurationElement dziedziczy z klasy bazowej ConfigurationElement . Obiekt ConfigurationElement reprezentuje element w pliku konfiguracji. Obiekt KeyValueConfigurationElement może należeć do KeyValueConfigurationCollection kolekcji.

Konstruktory

KeyValueConfigurationElement(String, String)

Inicjuje KeyValueConfigurationElement nowe wystąpienie klasy na podstawie podanych parametrów.

Właściwości

CurrentConfiguration

Pobiera odwołanie do wystąpienia najwyższego poziomu Configuration reprezentującego hierarchię konfiguracji, do którego należy bieżące ConfigurationElement wystąpienie.

(Odziedziczone po ConfigurationElement)
ElementInformation

ElementInformation Pobiera obiekt, który zawiera informacje i funkcje ConfigurationElement obiektu, które nie można dostosowywać.

(Odziedziczone po ConfigurationElement)
ElementProperty

ConfigurationElementProperty Pobiera obiekt reprezentujący ConfigurationElement sam obiekt.

(Odziedziczone po ConfigurationElement)
EvaluationContext

ContextInformation Pobiera obiekt dla ConfigurationElement obiektu.

(Odziedziczone po ConfigurationElement)
HasContext

Pobiera wartość wskazującą CurrentConfiguration , czy właściwość ma wartość null.

(Odziedziczone po ConfigurationElement)
Item[ConfigurationProperty]

Pobiera lub ustawia właściwość lub atrybut tego elementu konfiguracji.

(Odziedziczone po ConfigurationElement)
Item[String]

Pobiera lub ustawia właściwość, atrybut lub element podrzędny tego elementu konfiguracji.

(Odziedziczone po ConfigurationElement)
Key

Pobiera klucz KeyValueConfigurationElement obiektu.

LockAllAttributesExcept

Pobiera kolekcję zablokowanych atrybutów.

(Odziedziczone po ConfigurationElement)
LockAllElementsExcept

Pobiera kolekcję zablokowanych elementów.

(Odziedziczone po ConfigurationElement)
LockAttributes

Pobiera kolekcję zablokowanych atrybutów.

(Odziedziczone po ConfigurationElement)
LockElements

Pobiera kolekcję zablokowanych elementów.

(Odziedziczone po ConfigurationElement)
LockItem

Pobiera lub ustawia wartość wskazującą, czy element jest zablokowany.

(Odziedziczone po ConfigurationElement)
Properties

Pobiera kolekcję właściwości.

Value

Pobiera lub ustawia wartość KeyValueConfigurationElement obiektu.

Metody

DeserializeElement(XmlReader, Boolean)

Odczytuje kod XML z pliku konfiguracji.

(Odziedziczone po ConfigurationElement)
Equals(Object)

Porównuje bieżące ConfigurationElement wystąpienie z określonym obiektem.

(Odziedziczone po ConfigurationElement)
GetHashCode()

Pobiera unikatową wartość reprezentującą bieżące ConfigurationElement wystąpienie.

(Odziedziczone po ConfigurationElement)
GetTransformedAssemblyString(String)

Zwraca przekształconą wersję określonej nazwy zestawu.

(Odziedziczone po ConfigurationElement)
GetTransformedTypeString(String)

Zwraca przekształconą wersję określonej nazwy typu.

(Odziedziczone po ConfigurationElement)
GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
Init()

KeyValueConfigurationElement Ustawia obiekt na stan początkowy.

InitializeDefault()

Służy do inicjowania domyślnego zestawu wartości dla ConfigurationElement obiektu.

(Odziedziczone po ConfigurationElement)
IsModified()

Wskazuje, czy ten element konfiguracji został zmodyfikowany od czasu ostatniego zapisania lub załadowania, podczas implementacji w klasie pochodnej.

(Odziedziczone po ConfigurationElement)
IsReadOnly()

Pobiera wartość wskazującą, czy ConfigurationElement obiekt jest tylko do odczytu.

(Odziedziczone po ConfigurationElement)
ListErrors(IList)

Dodaje błędy nieprawidłowej właściwości w tym ConfigurationElement obiekcie i we wszystkich podelementach do przekazanej listy.

(Odziedziczone po ConfigurationElement)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
OnDeserializeUnrecognizedAttribute(String, String)

Pobiera wartość wskazującą, czy podczas deserializacji napotkano nieznany atrybut.

(Odziedziczone po ConfigurationElement)
OnDeserializeUnrecognizedElement(String, XmlReader)

Pobiera wartość wskazującą, czy podczas deserializacji napotkano nieznany element.

(Odziedziczone po ConfigurationElement)
OnRequiredPropertyNotFound(String)

Zgłasza wyjątek, gdy nie można odnaleźć wymaganej właściwości.

(Odziedziczone po ConfigurationElement)
PostDeserialize()

Wywoływana po deserializacji.

(Odziedziczone po ConfigurationElement)
PreSerialize(XmlWriter)

Wywoływana przed serializacji.

(Odziedziczone po ConfigurationElement)
Reset(ConfigurationElement)

Resetuje stan ConfigurationElement wewnętrzny obiektu, w tym blokady i kolekcje właściwości.

(Odziedziczone po ConfigurationElement)
ResetModified()

Resetuje wartość IsModified() metody w false przypadku implementacji w klasie pochodnej.

(Odziedziczone po ConfigurationElement)
SerializeElement(XmlWriter, Boolean)

Zapisuje zawartość tego elementu konfiguracji do pliku konfiguracji po zaimplementowaniu w klasie pochodnej.

(Odziedziczone po ConfigurationElement)
SerializeToXmlElement(XmlWriter, String)

Zapisuje zewnętrzne tagi tego elementu konfiguracji do pliku konfiguracji po zaimplementowaniu w klasie pochodnej.

(Odziedziczone po ConfigurationElement)
SetPropertyValue(ConfigurationProperty, Object, Boolean)

Ustawia właściwość na określoną wartość.

(Odziedziczone po ConfigurationElement)
SetReadOnly()

IsReadOnly() Ustawia właściwość dla ConfigurationElement obiektu i wszystkich podelementów.

(Odziedziczone po ConfigurationElement)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)
Unmerge(ConfigurationElement, ConfigurationElement, ConfigurationSaveMode)

Modyfikuje obiekt w ConfigurationElement celu usunięcia wszystkich wartości, które nie powinny być zapisywane.

(Odziedziczone po ConfigurationElement)

Dotyczy

Zobacz też