A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
File monitoring using FileSystemWatcher
Learn how to create your own file and folder monitoring application in C# using the FileSystemWatcher object. A sample application will be built in .NET 2.0 which can be used to monitor files, folders or drives.
On Tuesday, March 28th 2006 at 04:48 PM
By Andrew Pociu (View Profile)
(Rated 4.4 with 34 votes)
Download this project (C# 2005)
Wondering what's cranking your harddisk so hard? Using C# (.NET 1.1 or 2.0, whichever you prefer) and the FileSystemWatcher object you can easily build yourself a file monitoring application. You'll be able to see what files are being created, changed, renamed or deleted.
First we're going to write a few lines of code to quickly see how the FileSystemWatcher object operates. Then after we get the hang of it, we'll be building a simple Windows application that monitors a folder (or drive) for changes and writes the events to a log.
Working with FileSystemWatcherStart by creating a Windows Application in Visual Studio 2005. Note that Visual Studio 2003 and .NET 1.1 can also be used for this project, but the one attached to this tutorial is created in Visual Studio 2005 and .NET 2.0, thus you won't be able to open it using an earlier version.
Browse the toolbox for the FileSystemWatcher object, and drag it to the form.
Name it fileWatcher and since you're in the Properties window let's check the properties:
First, make sure the EnableRaisingEvents property is set to True otherwise the monitoring will not be enabled. The Filter property should either be set to *.* or to an empty string. This assures that we're not filtering any files, and we want to monitor each and every one of them.
Since we want to see some action, let's include subdirectories in the file monitoring, so set IncludeSubdirectories to True.
The NotifyFilter property should be left with its default value - FileName, DirectoryName, LastWrite.
As for the Path property, you'll probably want to enter C:\ since it's such a frequently accessed drive.
Now switch to the events view and you can see the 4 events of the FileSystemWatcher object. As their name implies, the events are being fired when a file is changed, created, deleted or renamed.
Doubleclick the first field (Changed) and you'll get to the event handler fileWatcher_Created(). Since this event is being fired when a file is changed, we want to be made aware of that. Thus, replace the event handler with the following code:
You can compile your application now. Once something is changed in C:\ a message box will popup showing the path to the file that suffered the change. It normally shouldn't take very long before this type of event occurs. You can try modifying a file on C:\ yourself, to see the event being fired.
You can do the same thing for the other events (Created, Deleted and Renamed). Double click them to get to the event handler, and paste the same MessageBox.Show() line.
The Rename event has two more properties unlike the other events: OldFullPath and OldName, which show the path / name of the file before it was renamed:
Now that you know how to use FileSystemWatcher to monitor a folder or a drive, let's create a Windows application where we can offer the user greater flexibility on monitoring his files.
private void fileWatcher_Changed(object sender, System.IO.FileSystemEventArgs e)
private void btnMonitor_Click(object sender, EventArgs e)
From the creators of Geekpedia, a revolutionary new coupon website!
BargainEZ has coupons codes, printable coupons, bargains and it is the leading source of Passbook coupons for iPhone and iPod touch devices.