Developer

Keep an eye on file system changes with VB.NET's FileSystemWatcher

The FileSystemWatcher component in Visual Studio .NET allows you to respond to file system changes. It's very handy whenever you need to perform a certain action in response to a file being updated.

FileSystemWatcher is a component in Visual Studio .NET that allows you to respond to file system changes. It's very handy whenever you need to perform a certain action in response to a file being updated and in numerous other situations.

Using FileSystemWatcher in your app

In order to add FileSystemWatcher to your Windows application, click on a Toolbox and find FileSystemWatcher under the list's Components section. Double-click FileSystemWatcher, and then the component will be visible under the Windows form (by default, it will be FileSystemWatcher1).

Now you are ready to set the properties of FileSystemWatcher1. This is how I set the following properties of the control through the Properties window in our example:

  • Filter: abc.txt
  • NotifyFilter: LastAccess
  • Path: C:\temp

Then I double-click FileSystemWatcher1 and add the following code to its Changed event:

        MessageBox.Show("file changed")

In order to get the example to work, you need to create a file in your c:/temp directory called abc.txt since the code assumes you already have this file. Then start your Windows application and -- while it's running -- open, edit, save, and close abc.txt. You should see the message box show up stating the file has changed. If you open the file but you don't save any changes to it, you will not see the message box since the NotifyFilter stated Write but not Open. In order to watch for changes in a particular directory, you would have to change the Filter property and the NotifyFilter property accordingly.

Irina Medvinskaya has been involved in technology since 1996. She has an MBA from Pace University and works as a project manager at Citigroup.

----------------------------------------------------------------------------------------

Get Visual Basic tips in your inbox

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically subscribe today!

3 comments
Richard.Schaefer
Richard.Schaefer

FileSystemWatcher is great if you have to watch existing files for changes (note: you most likely have to run it as service so if that causes problems for you then FileSystemWatcher isn't for you). For the task of waiting for files to show up in an empty directory I prefer to write a simple console program that I run every "n" minutes (i.e. 15 minutes) via Windows Started Tasks. I don't have to keep track of an installed service, I don't have those resources being consumed when the task isn't running, and the whole thing is much simpler.

slawrence
slawrence

It seems like having the NotifyFilter set to LastAccess would cause the message to be displayed when the file is opened. I tested it and you are correct in that the message is not displayed unless the file is changed. I wondered what happened if I changed the NotifyFilter to LastWrite and it ends up displaying the message box twice. How would you get notification that the file was opened and not changed? According to Microsoft documentation (http://msdn2.microsoft.com/en-us/library/system.io.notifyfilters(VS.71).aspx) if you use LastAccess you should get the message whenever there is a change to "The date the file or folder was last opened".

Justin James
Justin James

I have always liked this component for minor utilities, but I have always been cautious about it for system resource usage. Does using this component impose a serious hit on disk access performance? J.Ja

Editor's Picks