Export (0) Print
Expand All
2 out of 11 rated this helpful - Rate this topic

AppSettingsReader Class

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

Namespace:  System.Configuration
Assembly:  System (in System.dll)
public class AppSettingsReader

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

using System;
using System.Collections.Specialized;
using System.Configuration;


    class UsingAppSettingsReader
    {
        static void DisplayAppSettings()
        {

            try
            {

                AppSettingsReader reader = new AppSettingsReader();


                NameValueCollection appSettings = ConfigurationManager.AppSettings;

                for (int i = 0; i < appSettings.Count; i++)
                {
                    Console.WriteLine("Key : {0} Value: {1}",
                      appSettings.GetKey(i), appSettings[i]);
                }

            }
            catch (ConfigurationErrorsException e)
            {
                Console.WriteLine("[DisplayAppSettings: {0}]", e.ToString());
            }

        }

        static void CreateAppSettings()
        {
            try
            {
                // Get the count of the Application Settings. 
                int appSettingsCount = ConfigurationManager.AppSettings.Count;

                string asName = "Key" + appSettingsCount.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");
            }
            catch (ConfigurationErrorsException e)
            {
                Console.WriteLine("[CreateAppSettings: {0}]",
                    e.ToString());
            }
        }

        static void Main(string[] args)
        {

            string selection = "";

            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();
            }
        }
    }

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 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.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.