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#またはTryCast Visual Basic の関数。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.

適用対象

こちらもご覧ください