AppSettingsReader.GetValue Method (String, Type)

 
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 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.

Exception Condition
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
Available since 1.1
Return to top
Show: