VB/C# : How to use the FileSystem Watcher component

In this tutorial I'm going to teach you what you can do with the FileSystem Watcher component in visual basic/C# .
It is a standard component, so you don't need to import it to your project.

Start by opening a new project in C# or VB and call it FileSystemWatcher.
Once your project is fully loaded , add the fileSystem watcher component and a listbox :

Change the path of the filesystem Watcher in its properties to "C:\" or your standard drive.

Then go to the events of the Filesystem watcher by clicking the yellow lighting in the properties window:

 

As you can see, there are 4 events:

Changed
Created
Deleted
Renamed.

 

The event changed will tell you when a file is changed.
The event created will tell you when a file is created.
The event deleted will tell you when a file is deleted.
The event renamed will tell you when a filename is changed.

Double click one of these events, let's pick created .
Now add the following codes for the event created :

Private Sub FileSystemWatcher1_Created(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created

        ListBox1.Items.Add(e.FullPath + " was created !")

    End Sub

 

You can use the exact same code with the other 3 events: ListBox1.Items.Add(e.FullPath + " was created/deleted/removed/renamed!")

You do not need to add a timer to start the filesysystem watcher; it will start once your form is loaded.

 

 

For C# just add a ";" on the end of the code line

 

Download the project:FileSystemWatcher.rar (62,3 kB)


Topic: VB/C# : How to use the FileSystem Watcher component

Date: 26/04/2012

By: ccc

Subject: ccc

cccccc

Reply

—————