Configuration.GetSection(String) Configuration.GetSection(String) Configuration.GetSection(String) Configuration.GetSection(String) Method

定义

返回指定的 ConfigurationSection 对象。Returns the specified ConfigurationSection object.

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

参数

sectionName
String String String String

要返回的节的路径。The path to the section to be returned.

返回

指定的 ConfigurationSection 对象,如果请求的节不存在,则为 nullThe specified ConfigurationSection object, or null if the requested section does not exist.

示例

下面的示例演示如何使用GetSection方法访问自定义节。The following example shows how to use the GetSection method to access a custom section. 有关定义用于存储CustomSection部分信息的类的完整示例代码, Configuration请参阅类概述。For the complete example code that defines a class that stores information for the CustomSection section, see the Configuration class overview.

// 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

注解

为了方便起见,配置设置包含在将相似设置组合在一起的部分中。Configuration settings are contained within sections that group similar settings together for convenience. GetSection方法按名称检索配置节。The GetSection method retrieves a configuration section by its name.

适用于