AppSettingsSection Class
Assembly: System.Configuration (in system.configuration.dll)
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.
| Topic | Location |
|---|---|
| How to: Read Application Settings from the Web.config File | Configuring ASP .NET Web Applications |
| How to: Read Application Settings from the Web.config File | Configuring ASP .NET Web Applications |
The following code example shows how to use the AppSettingsSection object as a property of the ConfigurationManager class.
Imports System.Configuration Imports System.Collections.Specialized Module UsingAppSettingsSection Sub ShowAppSettings() Dim names As String() = ConfigurationManager.AppSettings.AllKeys Dim appStgs As NameValueCollection = ConfigurationManager.AppSettings Dim i As Integer For i = 0 To appStgs.Count - 1 Console.WriteLine("#{0 Name: {1 Value: {2", _ i, names(i), appStgs(i)) Next End Sub Sub Main() ' Uncomment the following line to set My.User to the currently logged on Windows user. ' My.User.InitializeWithWindowsUser Console.WriteLine("Application Settings Before:") ShowAppSettings() Console.WriteLine() Console.WriteLine("Add an Application Setting.") ' Get the count of the Application Settings. Dim appStgCnt As Integer = ConfigurationManager.AppSettings.Count Dim asName As String = "AppStg" + appStgCnt.ToString() ' Get the configuration file. Dim config As Configuration = _ 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() End Sub End Module
System.Configuration.ConfigurationElement
System.Configuration.ConfigurationSection
System.Configuration.AppSettingsSection
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.