언어

AppSettingsReader.GetValue(String, Type) 메서드

정의

속성에서 지정된 키의 AppSettings 값을 가져오고 구성에서 값을 포함하는 지정된 형식의 개체를 반환합니다.

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

매개 변수

key
String

값을 가져올 키입니다.

type
Type

반환할 개체의 형식입니다.

반품

지정된 키의 값입니다.

예외

key은 null입니다.

-또는-

type은 null입니다.

key 구성 섹션에 <appSettings> 없습니다.

-또는-

구성 섹션 <appSettings> 의 key 값은 형식type이 아닙니다.

예제

다음 예제에서는 메서드를 사용 하 여 GetValue 구성 파일의 섹션에서 각 키에 대 한 값을 검색 하는 <appSettings> 방법을 보여 있습니다.

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

적용 대상

추가 정보