.NET Framework Class Library
Configuration..::.GetSection Method

Updated: April 2009

Returns the specified ConfigurationSection object.

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

Visual Basic (Declaration)
Public Function GetSection ( _
    sectionName As String _
) As ConfigurationSection
Visual Basic (Usage)
Dim instance As Configuration
Dim sectionName As String
Dim returnValue As ConfigurationSection

returnValue = instance.GetSection(sectionName)
C#
public ConfigurationSection GetSection(
    string sectionName
)
Visual C++
public:
ConfigurationSection^ GetSection(
    String^ sectionName
)
JScript
public function GetSection(
    sectionName : String
) : ConfigurationSection

Parameters

sectionName
Type: System..::.String
The path to the section to be returned.
Remarks

Configuration settings are contained within sections that group similar settings together for convenience. The GetSection method retrieves a configuration section by its name.

Examples

The following code example demonstrates how to use the GetSection method to access custom section information. This example assumes that you have created a custom section class named CustomSection. For an example of such a class, see the Configuration class overview.

Visual Basic
' Get a custom section.
Shared Sub GetSection() 
    Try

        Dim customSection As CustomSection

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

        customSection = _
        config.GetSection("CustomSection")
        Console.WriteLine( _
        "Section name: {0}", _
        customSection.SectionInformation.Name)

    Catch err As ConfigurationErrorsException
        Console.WriteLine(err.ToString())
    End Try

End Sub 'GetSection

C#
// Get a custom section.
static void GetSection()
{
    try
    {

        CustomSection customSection;

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

        customSection = 
            config.GetSection("CustomSection") as CustomSection;

        Console.WriteLine("Section name: {0}", 
            customSection.SectionInformation.Name);

    }
    catch (ConfigurationErrorsException err)
    {
        Console.WriteLine(err.ToString());
    }

}
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0
See Also

Reference

Change History

Date

History

Reason

April 2009

Added a note about prerequisites for running the code example.

Customer feedback.

Tags :


Page view tracker