AppSettingsReader.GetValue(String, Type) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Gets the value for a specified key from the AppSettings property and returns an object of the specified type containing the value from the configuration.
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
Parameters
- key
- String
The key for which to get the value.
- type
- Type
The type of the object to return.
Returns
The value of the specified key.
Exceptions
key
does not exist in the <appSettings>
configuration section.
-or-
The value in the <appSettings>
configuration section for key
is not of type type
.
Examples
The following example shows how to use the GetValue method to retrieve the value for each key in the <appSettings>
section of the configuration file.
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
Applies to
See also
.NET
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기