Export (0) Print
Expand All

ConfigurationManager.AppSettings Property

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

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

'Declaration
Public Shared ReadOnly Property AppSettings As NameValueCollection
'Usage
Dim value As NameValueCollection 

value = ConfigurationManager.AppSettings

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.

' 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

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.

<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

Community Additions

ADD
Show:
© 2014 Microsoft