ARTICLE

Detecting File Changes using FileSystemWatcher

Posted by Thiagarajan Alagarsamy Articles | Visual Basic 2010 July 26, 2007
This article is about detecting file changes like file renaming, file creation, deletion and changes in a folder using FileSystemWatcher class.
Download Files:
 
Reader Level:

Introduction:

This article is about detecting file changes like file renaming, file creation, deletion and changes in a folder using FileSystemWatcher class.

FileSystemWatcher class comes in handy when we want to monitor a folder for any changes made. Consider we are having a production server, which needs to be monitored, and an email should be triggered when an unanticipated file change is encountered. In these circumstances FileSystemWatcher can be used to monitor files in the server.



Getting Started:

Before you start writing the first line of code for your file monitoring system, you should familiarize yourself with FileSystemWatcher class.

FileSystemWatcher listens to the file system change notifications and raises events when a directory, or file in a directory, changes. The component can watch files on a local computer, a network drive, or a remote computer.

The FileSystemWatcher provides us with the event handlers to capture events like renamed, deleted, created and changed.

Let us start by creating a windows application in Visual Studio .Net. Add a FolderBrowserDialog control to the form to get the folder path for which file monitoring is required. Now add a button to start the file monitoring. Add a click event handler to the button by double-clicking the button.

FileSystemWatcher:

Instantiate the FileSystemWatcher class by creating an object. Set the directory to watch by setting the path property.

Dim fwatcher As FileSystemWatcher = New FileSystemWatcher()

fwatcher.Path = txtFolder.Text


Type of changes to watch is set by the property NotifyFilter.


fwatcher.NotifyFilter=NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName


Now the FileSystemwatcher is set to watch for changes in LastWrite, LastAccess and FileName.

Add event handlers to capture events like Changed, Created, Deleted and Renamed.

 

AddHandler fwatcher.Changed, AddressOf Changed

AddHandler fwatcher.Created, AddressOf FileCreated

AddHandler fwatcher.Deleted, AddressOf Deleted

AddHandler fwatcher.Renamed, AddressOf Renamed


Files of a specific extension (like *.txt) can be watched by setting the Filter property to *.txt. This may be useful when you want to watch over the source code of a production box expect the log files. So that you can filter your watch by extensions.

The FileSystemWatcher can be programmatically controlled by enabling and disabling it using the EnableRaisingEvents property.

fwatcher.Filter = "*.txt"

fwatcher. EnableRaisingEvents = True

Capturing the change:

If we want to notify the administrator, that a file has been changed, we would require the name of the file to which the change has been made. To get the name of the file we use the event handler argument FileSystemEventArgs.

Private Sub Changed(ByVal sender As Object, ByVal e As FileSystemEventArgs)

          lblMessage.Text = e.FullPath.ToString() & " is changed!"

End Sub

 

FullPath property of the FileSystemEventArgs gives the full path of the file to which the change has happened. This can be displayed in a label to notify the administrator.

Conclusion:


FileSystemWatcher can be used to watch over files that contains sensitive information or source code that needs to be monitored for unauthorized changes. Although we can use Windows APIs to monitor a folder, using FileSystemWatcher class is simple and easy.

NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON (http://www.c-sharpcorner.com/).  

Login to add your contents and source code to this article
share this article :
post comment
 

how to use this class in ASP.NET ie for a web based application (c#). plz explain

Posted by ariez Jun 24, 2010

really thanks for this source!
it's the best I've found:
-it's clear;
-only the strict necessary;
-and not the less important, it really work fine, just download and run with VS!

Posted by lucas Mar 21, 2010
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor