ConfigurationManager.GetSection(String) Metode

Definisi

Mengambil bagian konfigurasi tertentu untuk konfigurasi default aplikasi saat ini.

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

Parameter

sectionName
String

Jalur dan nama bagian konfigurasi. Nama simpul dipisahkan dengan garis miring, misalnya "system.net/mailSettings/smtp".

Mengembalikan

Objek yang ditentukan ConfigurationSection , atau null jika bagian tidak ada.

Pengecualian

File konfigurasi tidak dapat dimuat.

Contoh

Contoh berikut menunjukkan cara menggunakan GetSection metode . Contohnya adalah bagian dari contoh yang lebih besar yang disediakan untuk ConfigurationManager kelas .

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

Keterangan

Untuk aplikasi klien, metode ini mengambil file konfigurasi yang diperoleh dengan menggabungkan file konfigurasi aplikasi, file konfigurasi pengguna lokal, dan file konfigurasi roaming.

Metode ini GetSection mengakses informasi konfigurasi run-time yang tidak dapat diubah. Untuk mengubah konfigurasi, Anda menggunakan GetSection metode pada file konfigurasi yang Anda peroleh dengan menggunakan salah satu metode berikut:

Catatan Bagi Inheritor

Anda harus melemparkan nilai yang dikembalikan ke jenis konfigurasi yang diharapkan. Untuk menghindari kemungkinan pengecualian transmisi, Anda harus menggunakan operasi transmisi kondisional seperti as operator di C# atau fungsi TryCast di Visual Basic.

Berlaku untuk

Lihat juga