AppSettingsReader.GetValue(String, Type) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
從 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
要傳回的物件型別。
傳回
指定索引鍵的值。
例外狀況
範例
下列範例示範如何使用 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