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
返すオブジェクトの型。
返品
指定したキーの値。
例外
key は、 <appSettings> 構成セクションに存在しません。
-または-
keyの<appSettings>構成セクションの値は、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