ConfigurationManager.RefreshSection(String) Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Menyegarkan bagian bernama sehingga kali berikutnya diambil, bagian tersebut akan dibaca ulang dari disk.
public:
static void RefreshSection(System::String ^ sectionName);
public static void RefreshSection (string sectionName);
static member RefreshSection : string -> unit
Public Shared Sub RefreshSection (sectionName As String)
Parameter
- sectionName
- String
Nama bagian konfigurasi atau jalur konfigurasi dan nama bagian bagian untuk di-refresh.
Contoh
Contoh kode berikut menunjukkan cara menggunakan RefreshSection metode untuk merefresh bagian konfigurasi pengaturan aplikasi.
// 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
Metode ini membatalkan cache untuk bagian konfigurasi yang ditentukan tanpa memengaruhi bagian lain.