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.

Cast your .NET This was published in Cast your .NET, check every Thursday for more stories

Related links

Comments

1

tc490225 - 25/10/07

I am looking for a way to actually open the file changed and display the contents to a richTextBox.. any suggestions? take a look at this? its a C# program, im looking for a way to do nearly the same thing with VB.NET

http://www.codeproject.com/cs/files/LogMonitor.asp

» Report offensive content

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

1

tc490225 - 25/10/07

I am looking for a way to actually open the file changed and display the contents to a richTextBox.. any suggestions? ... more

Log in


Sign up | Forgot your password?

  • Staff Shadow chasing in browsers

    The punching and counterpunching continued in the ongoing web browser development bout. Each time one browser closes a feature gap, a new feature appears in one of the others -- how we ever put up with the years of browser stagnation, I'll never know. Read more »

    -- posted by Staff

  • Chris Duckett Safari gets Gears

    Since its release in May last year, Gears has supported only Internet Explorer and Firefox browsers. With the addition of Safari into the Gears fold, it closes the loop of major browsers to support Gears Read more »

    -- posted by Chris Duckett

  • Renai LeMay MyPerfect.com.au has potential

    Victorian Web start-up My Perfect has a strong story and rationale for why it will succeed. But it has to overcome some challenges and design flaws first. Read more »

    -- posted by Renai LeMay

What's on?