AppSettingsSection.Settings Property

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets a collection of key/value pairs that contains application settings.

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

[ConfigurationPropertyAttribute("", IsDefaultCollection = true)]
public KeyValueConfigurationCollection Settings { get; }

Property Value

Type: System.Configuration.KeyValueConfigurationCollection

A collection of key/value pairs that contains the application settings from the configuration file.

The following example uses the Settings property to read the appSettings values.

   // This function shows how to read the key/value
   // pairs (settings collection)contained in the 
   // appSettings section.
   static void ReadAppSettings()
{
       try
       {

           // Get the configuration file.
           System.Configuration.Configuration config =
               ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

           // Get the appSettings section.
           System.Configuration.AppSettingsSection appSettings =
               (System.Configuration.AppSettingsSection)config.GetSection("appSettings");

           // Get the auxiliary file name.
           Console.WriteLine("Auxiliary file: {0}", config.AppSettings.File);


           // Get the settings collection (key/value pairs).
           if (appSettings.Settings.Count != 0)
           {
               foreach (string key in appSettings.Settings.AllKeys)
               {
                   string value = appSettings.Settings[key].Value;
                   Console.WriteLine("Key: {0} Value: {1}", key, value);
               }
           }
           else
               Console.WriteLine("The appSettings section is empty. Write first.");
       }
       catch (Exception e)
       {
           Console.WriteLine("Exception raised: {0}",
               e.Message);
       }
}

.NET Framework
Available since 2.0
Return to top
Show: