Export (0) Print
Expand All

AppSettingsSection Class

Note: This class is new in the .NET Framework version 2.0.

Provides configuration system support for the appSettings configuration section. This class cannot be inherited.

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

public sealed class AppSettingsSection : ConfigurationSection
public final class AppSettingsSection extends ConfigurationSection
public final class AppSettingsSection extends ConfigurationSection

The appSettings configuration section provides for key/value pairs of string values for an application. Rather than accessing these values using an instance of an AppSettingsSection object, you should use the AppSettings property of the ConfigurationManager class or the AppSettings property of the WebConfigurationManager class.

TopicLocation
How to: Read Application Settings from the Web.config FileConfiguring ASP .NET Web Applications
How to: Read Application Settings from the Web.config FileConfiguring ASP .NET Web Applications

The following code example shows how to use the AppSettingsSection object as a property of the ConfigurationManager class.

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

namespace ConfigurationSamples
{
  class UsingAppSettingsSection
  {
    static void ShowAppSettings()
    {
      string[] names = ConfigurationManager.AppSettings.AllKeys;
      NameValueCollection appStgs = ConfigurationManager.AppSettings;
      for (int i = 0; i < appStgs.Count; i++)
      {
        Console.WriteLine("#{0 Name: {1 Value: {2",
          i, names[i], appStgs[i]);
      
    

    static void Main(string[] args)
    {
      Console.WriteLine("Application Settings Before:");
      ShowAppSettings();
      Console.WriteLine();
      Console.WriteLine("Add an Application Setting.");

      // 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 After:");
      ShowAppSettings();
      Console.WriteLine();

      // Show the value of a named Application Setting.
      Console.WriteLine(
        "The value of application setting {0 is '{1'.",
        asName, ConfigurationManager.AppSettings[asName]);

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


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 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft