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 按其名称检索配置节。

适用于