ConfigurationManager.GetSection(String) ConfigurationManager.GetSection(String) ConfigurationManager.GetSection(String) ConfigurationManager.GetSection(String) Method

定義

現在のアプリケーションの既定構成の、指定した構成セクションを取得します。Retrieves a specified configuration section for the current application's default configuration.

public:
 static System::Object ^ GetSection(System::String ^ sectionName);
public static object GetSection (string sectionName);
static member GetSection : string -> obj
Public Shared Function GetSection (sectionName As String) As Object

パラメーター

sectionName
String String String String

構成セクションのパスと名前。The configuration section path and name. ノード名は、"system.net/mailSettings/smtp" のように、スラッシュで区切られます。Node names are separated by forward slashes, for example "system.net/mailSettings/smtp".

戻り値

指定した ConfigurationSection オブジェクト。セクションが存在しない場合は nullThe specified ConfigurationSection object, or null if the section does not exist.

例外

構成ファイルを読み込めませんでした。A configuration file could not be loaded.

GetSection メソッドを使用する方法の例を次に示します。The following example shows how to use the GetSection method. この例は、 ConfigurationManagerクラスに用意されている大規模な例の一部です。The example is part of a larger example that is provided for the ConfigurationManager class.

// Create the AppSettings section.
// The function uses the GetSection(string)method 
// to access the configuration section. 
// It also adds a new element to the section collection.
public static void CreateAppSettings()
{
  // Get the application configuration file.
  System.Configuration.Configuration config =
    ConfigurationManager.OpenExeConfiguration(
          ConfigurationUserLevel.None);

  string sectionName = "appSettings";

  // Add an entry to appSettings.
  int appStgCnt =
      ConfigurationManager.AppSettings.Count;
  string newKey = "NewKey" + appStgCnt.ToString();

  string newValue = DateTime.Now.ToLongDateString() + 
    " " + DateTime.Now.ToLongTimeString();

  config.AppSettings.Settings.Add(newKey, newValue);

  // Save the configuration file.
  config.Save(ConfigurationSaveMode.Modified);
  
  // Force a reload of the changed section. This 
  // makes the new values available for reading.
  ConfigurationManager.RefreshSection(sectionName);

  // Get the AppSettings section.
  AppSettingsSection appSettingSection =
    (AppSettingsSection)config.GetSection(sectionName);

  Console.WriteLine();
  Console.WriteLine("Using GetSection(string).");
  Console.WriteLine("AppSettings section:");
  Console.WriteLine(
    appSettingSection.SectionInformation.GetRawXml());
}
' Create the AppSettings section.
' The function uses the GetSection(string)method 
' to access the configuration section. 
' It also adds a new element to the section collection.
Public Shared Sub CreateAppSettings()
    ' Get the application configuration file.
    Dim config As System.Configuration.Configuration = _
    ConfigurationManager.OpenExeConfiguration( _
        ConfigurationUserLevel.None)

    Dim sectionName As String = "appSettings"

    ' Add an entry to appSettings.
    Dim appStgCnt As Integer = _
        ConfigurationManager.AppSettings.Count
    Dim newKey As String = _
        "NewKey" + appStgCnt.ToString()

    Dim newValue As String = _
        DateTime.Now.ToLongDateString() + " " + _
        DateTime.Now.ToLongTimeString()

    config.AppSettings.Settings.Add(newKey, _
                                    newValue)

    ' Save the configuration file.
    config.Save(ConfigurationSaveMode.Modified)

    ' Force a reload of the changed section. This 
    ' makes the new values available for reading.
    ConfigurationManager.RefreshSection(sectionName)

    ' Get the AppSettings section.
    Dim appSettingSection As AppSettingsSection = _
        DirectCast(config.GetSection(sectionName),  _
        AppSettingsSection)

    Console.WriteLine()
    Console.WriteLine( _
        "Using GetSection(string).")
    Console.WriteLine( _
        "AppSettings section:")
    Console.WriteLine( _
        appSettingSection.SectionInformation.GetRawXml())
End Sub

注釈

クライアントアプリケーションの場合、このメソッドは、アプリケーション構成ファイル、ローカルユーザー構成ファイル、およびローミング構成ファイルをマージすることによって取得された構成ファイルを取得します。For client applications, this method retrieves a configuration file obtained by merging the application configuration file, the local user configuration file, and the roaming configuration file.

メソッドGetSectionは、変更できない実行時の構成情報にアクセスします。The GetSection method accesses run-time configuration information that it cannot change. 構成を変更するには、次GetSectionのいずれかの Open メソッドを使用して、取得する構成ファイルに対してメソッドを使用します。To change the configuration, you use the GetSection method on the configuration file that you obtain by using one of the following Open methods:

注意 (継承者)

戻り値は、必要な構成の種類にキャストする必要があります。You must cast the return value to the expected configuration type. キャスト例外が発生する可能性を回避するには、のas C#演算子や Visual Basic のTryCast関数などの条件付きキャスト演算を使用する必要があります。To avoid possible casting exceptions, you should use a conditional casting operation such as the as operator in C# or the TryCast function in Visual Basic.

適用対象

こちらもご覧ください