共用方式為


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

要傳回的物件型別。

傳回

指定索引鍵的值。

例外狀況

keynull

-或-

typenull

<appSettings> 組態區段中沒有 key

-或-

<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

適用於

另請參閱