|
|
|
|
|
Home
»
Visual C#
»
Detecting File Changes using FileSystemWatcher
|
|
|
|
Technologies:
.NET 1.0/1.1,Visual C# .NET
|
|
Total downloads :
|
392
|
|
Total page views :
|
16192
|
|
Rating :
|
|
3/5
|
|
This article has been rated :
|
1 times
|
|
|
|
|
Download
Files:
|
|
|
|
|
|
|
|
|
|
Similar ArticlesMost ReadTop RatedLatest
|
|
Related EbooksTop Videos
|
|
|
Description
|
|
The Complete Visual C# Programmer's Guide, written by the authors of C# Corner, covers most of the major components that make up C# and the .NETenvironment including Windows Forms, ADO.NET, GDI+, Web Services, and Security. The book is geared toward the beginner to intermediate programmers.
|
|
|
|
|
|
|
|
|
|
|
|
|
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.

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
|
|
|
|
|
|
|
|
|
|
|
|
Thiagarajan Alagarsamy
Thiagu is living in Bangalore, India. His native is Madurai, a historic city in south India. He loves to code in C#. He frequents c# corner articles when he is not coding. Thiagu loves reading Jeffrey Archer and Sidney Sheldon novels. He is very much interested in Artificial Intelligence (AI). To view his blog - http://csharpnet.blogspot.com
|
|
|
|
|
|
|
|
|
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional
consulting company, our consultants are well-known experts in .NET and many of them
are MVPs, authors, and trainers. We specialize in Microsoft .NET development and
utilize Agile Development and Extreme Programming practices to provide fast pace
quick turnaround results. Our software development model is a mix of Agile Development,
traditional SDLC, and Waterfall models.
|
|
Click here to learn more about C# Consulting. |
|
|
|
|
|
|
|
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon.
Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees.
As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
|
Dynamic PDF
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.
|
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
|
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today. With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications. Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
|
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or
application via a range of API's. Learn More about our API connections.
|
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other
Visual Studio release. Work more productively and collaboratively-with
greater control over your work at every step. The Beta 2 can give you a
head start on achieving efficiency.
|
|
|
|
|
|
|
|
|
Download
Files:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|