Export (0) Print
Expand All

AppSettingsSection.Settings Property

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. 
Private Shared Sub ReadAppSettings()
    Try 

        ' Get the configuration file. 
        Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)

        ' Get the appSettings section. 
        Dim appSettings As System.Configuration.AppSettingsSection = CType(config.GetSection("appSettings"), System.Configuration.AppSettingsSection)

        ' 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 Then 
            For Each key As String In appSettings.Settings.AllKeys
                Dim value As String = appSettings.Settings(key).Value
                Console.WriteLine("Key: {0} Value: {1}", key, value)
            Next key
        Else
            Console.WriteLine("The appSettings section is empty. Write first.")
        End If 
    Catch e As Exception
        Console.WriteLine("Exception raised: {0}", e.Message)
    End Try 
End Sub
    // 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

Supported in: 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, 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.

Show:
© 2014 Microsoft