ConfigurationManager.GetSection(String) 方法

定義

擷取目前應用程式預設組態的指定組態區段。

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

組態區段路徑和名稱。 節點名稱以正斜線分隔,例如 "system.net/mailSettings/smtp"。

傳回

指定的 ConfigurationSection 物件;如果區段不存在,則為 null

例外狀況

無法載入組態檔。

範例

下列範例會示範如何使用 GetSection 方法。 此範例是針對 類別提供的較大範例的 ConfigurationManager 一部分。

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

備註

對於用戶端應用程式,這個方法會擷取藉由合併應用程式組態檔、本機使用者組態檔和漫遊組態檔取得的組態檔。

方法會 GetSection 存取無法變更的運行時間組態資訊。 若要變更組態,您可以使用 GetSection 下列其中一種方法,在取得的組態檔上使用 方法:

給繼承者的注意事項

您必須將傳回值轉換成預期的組態類型。 若要避免可能的轉換例外狀況,您應該使用條件式轉換作業,例如 as C# 中的 運算子或 Visual Basic 中的 TryCast 函式。

適用於

另請參閱