AppSettingsReader.GetValue(String, Type) Método

Definição

Obtém o valor de uma chave especificada da AppSettings propriedade e retorna um objeto do tipo especificado que contém o valor da configuração.

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

A chave para a qual obter o valor.

type
Type

O tipo do objeto a ser retornado.

Retornos

O valor da chave especificada.

Exceções

key é null.

-ou-

type é null.

key não existe na <appSettings> seção de configuração.

-ou-

O valor na <appSettings> seção de configuração para key o qual não é do tipo type.

Exemplos

O exemplo a <appSettings> seguir mostra como usar o GetValue método para recuperar o valor de cada chave na seção do arquivo de configuração.

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

Aplica-se a

Confira também