FileSystemWatcher is a class, that is part of the System.IO namespace in .NET.
FileSystemWatcher class allows user to monitor changes such as the creation, editing, renaming, or deletion of files within a given subdirectory.
Following example is given to call refreshSection method of ConfigurationManager to refresh AppSettings section of config file.
Following code is used to refresh config.exe file.
we can use FileSystemWatcher class for watching the config file and OnChange event of config file it will refresh that config file.
Code for using FileSystemWatcher class in VB.NET :
=========================================================
The following code should be written before the new values are saved to the config ‘file.
First you have to declare an object for FileSystemWatcher class.
Example:
==========================================================
Public Shared fileWatcher As FileSystemWatcher ' pathString holds the path of config file. Dim pathString As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) Dim filename As String = "SystemWatcher.exe.config" fileWatcher = New FileSystemWatcher() fileWatcher.Path = pathString fileWatcher.Filter = filename fileWatcher.NotifyFilter = (NotifyFilters.CreationTime Or NotifyFilters.LastWrite Or NotifyFilters.FileName) AddHandler fileWatcher.Changed, New FileSystemEventHandler(AddressOf OnChange) fileWatcher.EnableRaisingEvents = True =========================================================== ' On change event configuration file getting refreshed. Private Shared Sub OnChange(ByVal sender As Object, ByVal e As FileSystemEventArgs) ConfigurationManager.RefreshSection("AppSettings") End Sub
==================================================================