ConfigurationManager.AppSettings Property
Gets the AppSettingsSection data for the current application's default configuration.
Namespace: System.Configuration
Assembly: System.Configuration (in System.Configuration.dll)
Property Value
Type: System.Collections.Specialized.NameValueCollectionReturns a NameValueCollection object that contains the contents of the AppSettingsSection object for the current application's default configuration.
| Exception | Condition |
|---|---|
| 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 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.
<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 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.