Configuration.GetSection(String) 메서드

정의

지정된 ConfigurationSection 개체를 반환합니다.

public:
 System::Configuration::ConfigurationSection ^ GetSection(System::String ^ sectionName);
public System.Configuration.ConfigurationSection GetSection (string sectionName);
member this.GetSection : string -> System.Configuration.ConfigurationSection
Public Function GetSection (sectionName As String) As ConfigurationSection

매개 변수

sectionName
String

반환될 섹션의 경로입니다.

반환

지정된 ConfigurationSection 개체입니다. 또는 요청된 섹션이 없으면 null입니다.

예제

다음 예제에서는 메서드를 사용하여 GetSection 사용자 지정 섹션에 액세스하는 방법을 보여줍니다. 섹션에 대한 정보를 저장하는 클래스를 정의하는 전체 예제 코드는 CustomSection 클래스 개요를 Configuration 참조하세요.

// Show how to use the GetSection(string) method.
static void GetCustomSection()
{
    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.Name);
        Console.WriteLine("Url: {0}", customSection.Url);
        Console.WriteLine("Port: {0}", customSection.Port);
    }
    catch (ConfigurationErrorsException err)
    {
        Console.WriteLine("Using GetSection(string): {0}", err.ToString());
    }
}
' Show how to use the GetSection(string) method.
Public Shared Sub GetCustomSection()
    Try

        Dim customSection As CustomSection

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

        customSection = TryCast(config.GetSection("CustomSection"), CustomSection)

        Console.WriteLine("Section name: {0}", customSection.Name)
        Console.WriteLine("Url: {0}", customSection.Url)
        Console.WriteLine("Port: {0}", customSection.Port)

    Catch err As ConfigurationErrorsException
        Console.WriteLine("Using GetSection(string): {0}", err.ToString())
    End Try

End Sub

설명

구성 설정은 편의를 위해 유사한 설정을 함께 그룹화한 섹션 내에 포함되어 있습니다. 메서드는 GetSection 이름으로 구성 섹션을 검색합니다.

적용 대상