英語で読む

次の方法で共有


Configuration.GetSection(String) メソッド

定義

指定した ConfigurationSection オブジェクトを返します。

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

パラメーター

sectionName
String

返されるセクションへのパス。

戻り値

指定した ConfigurationSection オブジェクト。または、要求したセクションが存在しない場合は null

次の例では、 メソッドを使用 GetSection してカスタム セクションにアクセスする方法を示します。 セクションの情報を格納するクラスを定義する完全なコード例については、クラスの概要に関 CustomSection するページを Configuration 参照してください。

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

注釈

構成設定は、便宜上同様の設定をグループ化するセクションに含まれています。 メソッドは GetSection 、その名前で構成セクションを取得します。

適用対象

製品 バージョン
.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
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9