Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
Configuration Class
 GetSection Method
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
Configuration..::.GetSection Method

Updated: November 2007

Returns the specified ConfigurationSection object.

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

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
)
J#
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.

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

The following code example demonstrates how to use the GetSection method to access custom section information.

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());
    }

}

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, 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.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker