This documentation is archived and is not being maintained.

ConfigurationManager.GetSection Method

Note: This method is new in the .NET Framework version 2.0.

Retrieves a specified configuration section for the current application's default configuration.

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

public static Object GetSection (
	string sectionName
)
public static Object GetSection (
	String sectionName
)
public static function GetSection (
	sectionName : String
) : Object

Parameters

sectionName

The configuration section path and name.

Return Value

The specified ConfigurationSection object, or a null reference (Nothing in Visual Basic) if the section does not exist.

Exception typeCondition

ConfigurationErrorsException

A configuration file could not be loaded.

For client applications, this method retrieves a configuration file obtained by merging the application configuration file, the local user configuration file, and the roaming configuration file.

The GetSection method accesses run-time configuration information that it cannot change. To change the configuration, you use the GetSection method on the configuration file you obtain by using one of the following Open methods:

Notes to Implementers You must cast the return value to the expected configuration type. To avoid possible casting exceptions, you should use a conditional casting operation such as the as operator in C# or the TryCast function in Visual Basic.

The following code example shows how to use the GetSection method to create a configuration custom section and save it to the application configuration file.

// Get the a custom section. Show how to use the
// GetSection method.
static void GetCustomSection()
{
    // Get the custom section.
    CustomSection custSection = 
        ConfigurationManager.GetSection("CustomSection") as CustomSection;
    
    // Read the custom section.
    Console.WriteLine("File name: {0 MaxIdleTime: {1 MaxUsers: {2",
        custSection.FileName, custSection.MaxIdleTime, 
        custSection.MaxUsers);



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.

.NET Framework

Supported in: 2.0
Show: