We need you!

We're working hard on the next version of Developer Fusion. Let us know what you think we should be up to!

Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 34,778 times

Contents

Related Categories

Watching Folder Activity in VB.NET - Building Our Application

JayeshJain

Building Our Application

The FileSystemWatcher class raises five events, which are Created, Changed, Deleted, Renamed and Error. Because Created, Changed, and Deleted events share the same event signature, we can write just one event handler. We will also write one event handler for Renamed, because its event signatures are different.

Lets type code for handling Created, Changed, and Deleted events raised by the FileSystemWatcher class.

Note: You will have to type the event declaration, as this procedure is not generated automatically

Private Sub logchange(ByVal source As Object, ByVal e As _
System.IO.FileSystemEventArgs)
If e.ChangeType = IO.WatcherChangeTypes.Changed Then
txt_folderactivity.Text &= "File " & e.FullPath & _
" has been modified" & vbCrLf
End If
If e.ChangeType = IO.WatcherChangeTypes.Created Then
txt_folderactivity.Text &= "File " & e.FullPath & _
" has been created" & vbCrLf
End If
If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
txt_folderactivity.Text &= "File " & e.FullPath & _
" has been deleted" & vbCrLf
End If
End Sub

This is the code for handling the Renamed event, risen by the FileSystemWatcher class:

Public Sub logrename(ByVal source As Object, ByVal e As _
System.IO.RenamedEventArgs)
txt_folderactivity.Text &= "File" & e.OldName & _
" has been renamed to " & e.Name & vbCrLf
End Sub

Lastly, here's is the code for the btn_stop_click, which will stop the monitor:

' Stop watching the folder
watchfolder.EnableRaisingEvents = False
btn_startwatch.Enabled = True
btn_stop.Enabled = False

Testing Our Application

Now it's the time to run the application and see it in action. Build and run the application, type the folder you want to monitor into the text box and click start watching to start watching that folder.

In the folder you specified, create a file, rename it, update it and delete it to see our application recording those changes, as shown below:

Useful Information

Use the FileSystemWatcher.Filter property to determine what files should be monitored. Setting the filter property to "*.txt" will monitor all files with a txt extension. The default is *.*, which means that all files will be monitored. If you want to monitor all files -- with and without extensions -- set the Filter property to "".

FileSystemWatcher can be used to watch files on a local computer, a network drive, or a remote computer, but it does not raise events for a CDROM drive. It only works on Windows 2000 and Windows NT 4.0, and common file system operations might raise more than one event. For example, when a file is edited or moved, more than one event might be raised. Likewise, some anti virus or other monitoring applications can cause additional events.

The FileSystemWatcher class will not watch the specified folder until the path property has been set and EnableRaisingEvents is set to true. Set the FileSystemWatcher.IncludeSubdirectories property to true if you want to monitor subdirectories; otherwise, set it to false. The default is false.

The FileSystemWatcher.Path property supports universal naming convention (UNC) paths. If the folder that the path points to is renamed, then the FileSystemWatcher will reattached itself to the new renamed folder – a very handy function indeed!

Jayesh Jain is working as a Business Analyst in Auckland, New Zealand. He has several years of n-Tier development experience in developing interactive client solutions. He has a passion for Web development and in the spare time he likes to write articles. Contact him at: jainjayesh74@yahoo.com

Comments

  • Re: Cross-Threading

    Posted by D'Scouser on 06 Feb 2008

    In VB2005 there is a painful cross-threading error that throws an InvalidOperationException when returning text from the fileSystemWatcher to the main form. This can be resolved by ...

  • One way...

    Posted by wduros1 on 04 Dec 2005

    Although the file watcher provides notification of a file create, it provides this notification at the moment of the file "creation", not the moment of the file "completion" if you know what I mean. ...

  • Running this code as a service

    Posted by tlombardi on 26 Sep 2005

    Hi I have tried this code to use it for watching a folder for tif files which I wish to print automatically. I have managed to get it to work in an application mode. However I cant seem to make it wor...

  • Posted by jwillis27640 on 12 Jul 2005

    Thanks JMurDock!! Maybe you can help me with this. I am using Excel 2003 to parse the XML because I don't know what information might be in the file. It has varying field information so I do it like t...

  • Posted by jmurdock on 12 Jul 2005

    [quote][1]Posted by [b]jwillis27640[/b] on 12 Jul 2005 05:14 PM[/1]
    What are you doing with your service? I need the same type of thing but I need to parse the xml and sent it to SQL server. I have d...