Share via


AppSettingsReader.GetValue(String, Type) Método

Definición

Obtiene el valor de una clave especificada de la propiedad AppSettings y devuelve un objeto del tipo especificado que contiene el valor de la configuración.

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

Parámetros

key
String

Clave para la que se obtiene el valor.

type
Type

El tipo de objeto que se va a devolver.

Devoluciones

Valor de la clave especificada.

Excepciones

key es null.

O bien

type es null.

El valor key no existe en la sección de configuración de <appSettings>.

o bien

El valor de la sección de configuración <appSettings> de key no es del tipo type.

Ejemplos

En el ejemplo siguiente se muestra cómo usar el GetValue método para recuperar el valor de cada clave de la <appSettings> sección del archivo de configuración.

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

Se aplica a

Consulte también