英語で読む 編集

次の方法で共有


Configuration.GetSection(String) Method

Definition

Returns the specified ConfigurationSection object.

C#
public System.Configuration.ConfigurationSection GetSection(string sectionName);

Parameters

sectionName
String

The path to the section to be returned.

Returns

The specified ConfigurationSection object, or null if the requested section does not exist.

Examples

The following example shows how to use the GetSection method to access a custom section. For the complete example code that defines a class that stores information for the CustomSection section, see the Configuration class overview.

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

Remarks

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

Applies to

製品 バージョン
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10