ARTICLE

Detecting File Changes using FileSystemWatcher

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

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 a unanticipated file change is encountered. In these circumstance FileSystemWatcher can be used to monitor files in the server.

FileSystemWatcher.JPG

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 a object. Set the directory to watch by setting the path property.

FileSystemWatcher fwatcher = new FileSystemWatcher();

fwatcher.Path = txtFolder.Text;

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


fwatcher.NotifyFilter=NotifyFilters.LastAccess | NotifyFilters.LastWrite | 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.

fwatcher.Changed += new FileSystemEventHandler(Changed);
fwatcher.Created += new FileSystemEventHandler(FileCreated);
fwatcher.Deleted += new FileSystemEventHandler(Deleted);
fwatcher.Renamed += new RenamedEventHandler(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 void Changed(object sender, FileSystemEventArgs e)

{

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

}

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.

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

It not monitor the samba server folder..if any solution for that...pls kindly update.....

Posted by Nagarajan K Feb 28, 2013

My problem is: I want to read the file. Then I assign the read-file function to the Changed Event and got exception that the filed is locked by another process, any idea?

Posted by Jali Jack Dec 04, 2009

just insert this line in the public void for each change above the lblmessage.text... line CheckForIllegalCrossThreadCalls = false;

Posted by Cameron Mitchell Apr 02, 2007

After downloading the source code to this article, I attempted to run it as is. Within the application I browsed to a folder and clicked on Watch. When I added a next text document, I get the following error: Cross-thread operation not valid: Control 'lblMessage' accessed from a thread other than the thread it was created on.

Posted by Rodney Mar 22, 2007

After downloading the source code to this article, I attempted to run it as is. Within the application I browsed to a folder and clicked on Watch. When I added a next text document, I get the following error: Cross-thread operation not valid: Control 'lblMessage' accessed from a thread other than the thread it was created on.

Posted by Rodney Mar 22, 2007
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter