System.Configuration Namesp ...


.NET Framework Class Library
AppSettingsReader Class

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

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

Visual Basic (Declaration)
Public Class AppSettingsReader
Visual Basic (Usage)
Dim instance As AppSettingsReader
C#
public class AppSettingsReader
Visual C++
public ref class AppSettingsReader
JScript
public class AppSettingsReader
Examples

The following example creates a configuration file that contains the <appSettings> section, and then uses the AppSettingsReader to read the settings just generated.

Visual Basic
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

C#
using System;
using System.Collections.Specialized;
using System.Configuration;

namespace AspNet.Samples
{
    class UsingAppSettingsSection
    {
        static void ShowAppSettings()
        {


            AppSettingsReader reader = 
                new AppSettingsReader();


            NameValueCollection appStgs = 
                ConfigurationManager.AppSettings;

            string[] names = 
                ConfigurationManager.AppSettings.AllKeys;

            String value = String.Empty;

            for (int i = 0; i < appStgs.Count; i++)
            {


                string key = names[i];

                value = (String)reader.GetValue(key, value.GetType());

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

            }

        }

        static void Main(string[] args)
        {

            // Get the count of the Application Settings.
            int appStgCnt = ConfigurationManager.AppSettings.Count;

            string asName = "AppStg" + appStgCnt.ToString();

            // Get the configuration file.
            System.Configuration.Configuration config =
              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();
        }
    }
}

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>
Inheritance Hierarchy

System..::.Object
  System.Configuration..::.AppSettingsReader
Thread Safety

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

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

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
See Also

Reference

Other Resources

Tags :


Page view tracker