Share via


AppSettingsReader.GetValue(String, Type) Método

Definição

Obtém o valor de uma chave especificada da propriedade AppSettings 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 o valor será obtido.

type
Type

O tipo de objeto a ser retornado.

Retornos

O valor da coluna especificada.

Exceções

key é null.

- ou -

type é null.

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

- ou -

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

Exemplos

O exemplo a seguir mostra como usar o GetValue método para recuperar o valor de cada chave na <appSettings> 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