AppSettingsReader Class
Provides a method for reading values of a particular type from the configuration.
Namespace: System.Configuration
Assembly: System (in System.dll)
The AppSettingsReader type exposes the following members.
| Name | Description | |
|---|---|---|
![]() | Equals(Object) | Determines whether the specified object is equal to the current object. (Inherited from Object.) |
![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() | GetValue | 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. |
![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
The following example creates a configuration file that contains the <appSettings> section, and then uses the AppSettingsReader to read the settings just generated.
Imports System Imports System.Collections.Specialized Imports System.Configuration Class UsingAppSettingsReader Private Shared Sub DisplayAppSettings() Try Dim reader As New System.Configuration.AppSettingsReader() Dim appSettings As NameValueCollection = ConfigurationManager.AppSettings For i As Integer = 0 To appSettings.Count - 1 Console.WriteLine("Key : {0} Value: {1}", appSettings.GetKey(i), appSettings(i)) Next i Catch e As ConfigurationErrorsException Console.WriteLine("[DisplayAppSettings: {0}]", e.ToString()) End Try End Sub Private Shared Sub CreateAppSettings() Try ' Get the count of the Application Settings. Dim appSettingsCount As Integer = ConfigurationManager.AppSettings.Count Dim asName As String = "Key" & appSettingsCount.ToString() ' Get the configuration file. Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) ' Add an Application setting. config.AppSettings.Settings.Add(asName, Date.Now.ToLongDateString() & " " & Date.Now.ToLongTimeString()) ' Save the configuration file. config.Save(ConfigurationSaveMode.Modified) ' Force a reload of a changed section. ConfigurationManager.RefreshSection("appSettings") Catch e As ConfigurationErrorsException Console.WriteLine("[CreateAppSettings: {0}]", e.ToString()) End Try End Sub Shared Sub Main(ByVal args() As String) Dim selection As String = "" Do While selection.ToLower() <> "q" ' Create appSettings section. CreateAppSettings() ' Display appSettings section. DisplayAppSettings() Console.WriteLine() Console.WriteLine("Enter 'Q' to exit or press enter to continue.") Console.Write("> ") selection = Console.ReadLine() Loop End Sub End Class
The following example demonstrates a configuration file used by the previous example.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="AppStg0" value="Thursday, December 01, 2005 12:53:56 PM" />
<add key="AppStg1" value="Thursday, December 01, 2005 12:54:15 PM" />
<add key="AppStg2" value="Thursday, December 01, 2005 1:37:22 PM" />
</appSettings>
</configuration>
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.gif?cs-save-lang=1&cs-lang=vb)
.gif?cs-save-lang=1&cs-lang=vb)