AppSettingsReader.GetValue(String, Type) Methode

Definitie

Haalt de waarde voor een opgegeven sleutel op uit de AppSettings eigenschap en retourneert een object van het opgegeven type dat de waarde uit de configuratie bevat.

public:
 System::Object ^ GetValue(System::String ^ key, Type ^ type);
public object GetValue(string key, Type type);
member this.GetValue : string * Type -> obj
Public Function GetValue (key As String, type As Type) As Object

Parameters

key
String

De sleutel waarvoor de waarde moet worden opgehaald.

type
Type

Het type object dat moet worden geretourneerd.

Retouren

De waarde van de opgegeven sleutel.

Uitzonderingen

key is null.

– of –

type is null.

key bestaat niet in de <appSettings> configuratiesectie.

– of –

De waarde in de <appSettings> configuratiesectie voor key is niet van het type type.

Voorbeelden

In het volgende voorbeeld ziet u hoe u de GetValue methode gebruikt om de waarde voor elke sleutel op te halen in de <appSettings> sectie van het configuratiebestand.

static void DisplayAppSettings()
{
    try
    {
        var reader = new AppSettingsReader();

        NameValueCollection appSettings = ConfigurationManager.AppSettings;

        for (int i = 0; i < appSettings.Count; i++)
        {
            string key = appSettings.GetKey(i);
            string value = (string)reader.GetValue(key, typeof(string));
            Console.WriteLine("Key : {0} Value: {1}", key, value);
        }
    }
    catch (ConfigurationErrorsException e)
    {
        Console.WriteLine("[DisplayAppSettings: {0}]", e.ToString());
    }
}
Private Shared Sub DisplayAppSettings()

    Try
        Dim reader As New AppSettingsReader()

        Dim appSettings As NameValueCollection = ConfigurationManager.AppSettings

        For i As Integer = 0 To appSettings.Count - 1
            Dim key As String = appSettings.GetKey(i)
            Dim value As String = reader.GetValue(key, GetType(String))
            Console.WriteLine("Key : {0} Value: {1}", key, value)
        Next i

    Catch e As ConfigurationErrorsException
        Console.WriteLine("[DisplayAppSettings: {0}]", e.ToString())
    End Try

End Sub

Van toepassing op

Zie ook