Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 35,042 times

Contents

Related Categories

Watching Folder Activity in VB.NET - Getting Started

JayeshJain

Getting Started

Open Visual Studio .NET and create a new Windows Application Project. Call it WatchFolder and click OK:

Create a user interface as shown in the image below:

Add the following controls to our form:

  • txt_watchpath: TextBox (for folder path)
  • btn_startwatch: Button (start watching)
  • btn_stop: Button (stop watching)
  • txt_folderactivity: Textbox (folder activity)

Lets start coding for this application. The first thing we need to do is to import the required classes. Type the following code before your class declaration:

Imports System.IO
Imports System.Diagnostics

This code will import the necessary class required for our application. We also need to declare a public variable for our FileSystemWatcher class:

Public watchfolder As FileSystemWatcher

Add the following code to the btn_start_click procedure:

watchfolder = New System.IO.FileSystemWatcher()

'this is the path we want to monitor
watchfolder.Path = txt_watchpath.Text

'Add a list of Filter we want to specify
'make sure you use OR for each Filter as we need to
'all of those

watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.FileName
watchfolder.NotifyFilter = watchfolder.NotifyFilter Or _
IO.NotifyFilters.Attributes

' add the handler to each event
AddHandler watchfolder.Changed, AddressOf logchange
AddHandler watchfolder.Created, AddressOf logchange
AddHandler watchfolder.Deleted, AddressOf logchange

' add the rename handler as the signature is different
AddHandler watchfolder.Renamed, AddressOf logrename

'Set this property to true to start watching
watchfolder.EnableRaisingEvents = True

btn_startwatch.Enabled = False
btn_stop.Enabled = True

'End of code for btn_start_click

The NotifyFilter propery is used to specify the types of changes that we want to watch. You can combine the notify filters to watch for one or more types of changes. For example, set the NotifyFilter property to Size if you want to monitor the changes in the file/folder size.

There are many NotifyFilter properties that you can set. Here’s the complete list:

  • Attributes: The attributes of the file or folder
  • CreationTime: The time the file or folder was created
  • DirectoryName: The name of the directory
  • FileName: The name of the file
  • LastAccess: The date the file or folder was last opened
  • LastWrite: The date the file or folder last had anything written to it
  • Security: The security settings of the file or folder
  • Size: The size of the file or folder

The default is the bitwise OR combination of LastWrite, FileName, and DirectoryName.

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...