Export (0) Print
Expand All

AppSettingsReader Class

Provides a method for reading values of a particular type from the configuration.

Namespace: System.Configuration
Assembly: System (in system.dll)

'Declaration
Public Class AppSettingsReader
'Usage
Dim instance As AppSettingsReader

public class AppSettingsReader
public class AppSettingsReader
Not applicable.

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

Namespace AspNet.Samples

    Class UsingAppSettingsSection


        Shared Sub ShowAppSettings()


            Dim reader As New AppSettingsReader()


            Dim appStgs As NameValueCollection = _
            ConfigurationManager.AppSettings

            Dim names As String() = _
            ConfigurationManager.AppSettings.AllKeys

            Dim value As String = String.Empty

            Dim i As Integer
            For i = 1 To appStgs.Count - 1
                Dim key As String = names(i)

                value = CStr(reader.GetValue(key, value.GetType()))

                Console.WriteLine("#{0} Name: {1} Value: {2}", i, key, value)
            Next i

        End Sub 'ShowAppSettings


        Public Shared Sub Main(ByVal args() As String)

            ' Get the count of the Application Settings.
            Dim appStgCnt As Integer = ConfigurationManager.AppSettings.Count

            Dim asName As String = "AppStg" + appStgCnt.ToString()

            ' Get the configuration file.
            Dim config As System.Configuration.Configuration = _
            ConfigurationManager.OpenExeConfiguration( _
            ConfigurationUserLevel.None)

            ' Add an Application setting.
            config.AppSettings.Settings.Add(asName, _
            DateTime.Now.ToLongDateString() + " " + _
            DateTime.Now.ToLongTimeString())

            ' Save the configuration file.
            config.Save(ConfigurationSaveMode.Modified)

            ' Force a reload of a changed section.
            ConfigurationManager.RefreshSection("appSettings")


            Console.WriteLine()
            Console.WriteLine("Application Settings:")
            ShowAppSettings()
            Console.WriteLine()

            Console.WriteLine("Press 'Enter' to exit.")
            Console.ReadLine()

        End Sub 'Main
    End Class 'UsingAppSettingsSection


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>

System.Object
  System.Configuration.AppSettingsReader

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft