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.

异常

示例

下面的示例显示如何使用 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. 若要避免可能的转换异常,应在 Visual Basic 中使用条件转换运算as (如C#中的运算符或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.

适用于

另请参阅