WebConfigurationManager.AppSettings Property

Definition

Gets the Web site's application settings.

C#
public static System.Collections.Specialized.NameValueCollection AppSettings { get; }

Property Value

A NameValueCollection object that contains the AppSettingsSection object for the current Web application's default configuration.

Exceptions

A valid NameValueCollection object could not be retrieved with the application settings data.

Examples

The following example shows how to access configuration information with the AppSettings method.

C#

// Show the use of the AppSettings property
// to get the application settings.
static void GetAppSettings()
{

    // Get the appSettings key,value pairs collection.
    NameValueCollection appSettings =
        WebConfigurationManager.AppSettings
        as NameValueCollection;

    // Get the collection enumerator.
    IEnumerator appSettingsEnum =
        appSettings.GetEnumerator();

    // Loop through the collection and
    // display the appSettings key, value pairs.
    int i = 0;
    Console.WriteLine("[Display appSettings]");
    while (appSettingsEnum.MoveNext())
    {
        string key = appSettings.AllKeys[i].ToString();
        Console.WriteLine("Key: {0} Value: {1}",
        key, appSettings[key]);
        i += 1;
    }

    Console.WriteLine();
}

Remarks

An AppSettingsSection object contains the configuration file's <appSettings> section.

Applies to

Produit Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

See also