Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
 AppSettings Property
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
ConfigurationManager..::.AppSettings Property

Gets the AppSettingsSection data for the current application's default configuration.

Namespace:  System.Configuration
Assembly:  System.Configuration (in System.Configuration.dll)
Visual Basic (Declaration)
Public Shared ReadOnly Property AppSettings As NameValueCollection
Visual Basic (Usage)
Dim value As NameValueCollection

value = ConfigurationManager.AppSettings
C#
public static NameValueCollection AppSettings { get; }
Visual C++
public:
static property NameValueCollection^ AppSettings {
    NameValueCollection^ get ();
}
JScript
public static function get AppSettings () : NameValueCollection

Property Value

Type: System.Collections.Specialized..::.NameValueCollection
Returns a NameValueCollection object that contains the contents of the AppSettingsSection object for the current application's default configuration.
ExceptionCondition
ConfigurationErrorsException

Could not retrieve a NameValueCollection object with the application settings data.

A AppSettingsSection object contains the contents of the configuration file's appSettings section.

The following example shows how to use the AppSettings property. It is part of a larger example that is provided for the ConfigurationManager class.

Visual Basic
' Get the AppSettings section.        
' This function uses the AppSettings property
' to read the appSettings configuration 
' section.
Public Shared Sub ReadAppSettings()
    Try
        ' Get the AppSettings section.
        Dim appSettings As NameValueCollection = _
            ConfigurationManager.AppSettings

        ' Get the AppSettings section elements.
        Console.WriteLine()
        Console.WriteLine("Using AppSettings property.")
        Console.WriteLine("Application settings:")

        If appSettings.Count = 0 Then
            Console.WriteLine( _
            "[ReadAppSettings: {0}]", _
            "AppSettings is empty Use GetSection first.")
        End If
        Dim i As Integer = 0
        While i < appSettings.Count
            Console.WriteLine( _
                "#{0} Key: {1} Value: {2}", _
                i, appSettings.GetKey(i), appSettings(i))
            System.Math.Max( _
                System.Threading.Interlocked. _
                Increment(i), i - 1)
        End While
    Catch e As ConfigurationErrorsException
        Console.WriteLine("[ReadAppSettings: {0}]", _
                          e.ToString())
    End Try
End Sub
C#
// Get the AppSettings section.        
// This function uses the AppSettings property
// to read the appSettings configuration 
// section.
public static void ReadAppSettings()
{
  try
  {
    // Get the AppSettings section.
    NameValueCollection appSettings =
       ConfigurationManager.AppSettings;

    // Get the AppSettings section elements.
    Console.WriteLine();
    Console.WriteLine("Using AppSettings property.");
    Console.WriteLine("Application settings:");

    if (appSettings.Count == 0)
    {
      Console.WriteLine("[ReadAppSettings: {0}]",
      "AppSettings is empty Use GetSection command first.");
    }
    for (int i = 0; i < appSettings.Count; i++)
    {
      Console.WriteLine("#{0} Key: {1} Value: {2}",
        i, appSettings.GetKey(i), appSettings[i]);
    }
  }
  catch (ConfigurationErrorsException e)
  {
    Console.WriteLine("[ReadAppSettings: {0}]",
        e.ToString());
  }
}

The example works with configuration elements that are similar to the ones illustrated in the following configuration file. These elements are generated the first time you run the example.

None
<appSettings>
      <add key="NewKey0" value="Monday, March 30, 
           2009 1:36:33 PM" />
      <add key="NewKey1" value="Monday, March 30, 
           2009 1:36:40 PM" />
  </appSettings>

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
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Simple Usage Example - Access AppSetting by key      rtpHarry   |   Edit   |   Show History
The sample in the document doesn't show you how to directly access a property you know the name of.

Given the following <appSettings> snippet in your web.config:
<!-- put this right after </configSections> -->
<appSettings>
<add key="AdminEmail" value="admin@example.com"/>
</appSettings>
<!-- this should be just before <connectionStrings> -->
Then you can access it like this:
C#:
string AdminEmail = ConfigurationManager.AppSettings["AdminEmail"];

VB.NET:
Dim AdminEmail As String = ConfigurationManager.AppSettings("AdminEmail")


Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker