Export (0) Print
Expand All

AppSettingsReader.GetValue Method

Gets the value for a specified key from the AppSettings property and returns an object of the specified type containing the value from the configuration.

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

public Object GetValue(
	string key,
	Type type
)

Parameters

key
Type: System.String

The key for which to get the value.

type
Type: System.Type

The type of the object to return.

Return Value

Type: System.Object
The value of the specified key.

ExceptionCondition
ArgumentNullException

key is null.

- or -

type is null.

InvalidOperationException

key does not exist in the <appSettings> configuration section.

- or -

The value in the <appSettings> configuration section for key is not of type type.

The following example shows how to use the GetValue method to retrieve the value for each key in the <appSettings> section of the configuration file.

static void DisplayAppSettings()
{

    try
    {

        AppSettingsReader reader = new AppSettingsReader();


        NameValueCollection appSettings = ConfigurationManager.AppSettings;

        for (int i = 0; i < appSettings.Count; i++)
        {
            Console.WriteLine("Key : {0} Value: {1}",
              appSettings.GetKey(i), appSettings[i]);
        }

    }
    catch (ConfigurationErrorsException e)
    {
        Console.WriteLine("[DisplayAppSettings: {0}]", e.ToString());
    }

}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.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