SunQuest
 
       VB.Net
  Home arrow VB.Net arrow Page 3 - Watching Folder Activity in VB.NET
Moblin
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VB.NET

Watching Folder Activity in VB.NET
By: Jayesh Jain
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 54
    2002-10-13

    Table of Contents:
  • Watching Folder Activity in VB.NET
  • Getting Started
  • Building Our Application (contd.)
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Watching Folder Activity in VB.NET - Building Our Application (contd.)


    (Page 3 of 4 )

    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 [End Note]

    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:

    Testing our application

    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!

    More VB.Net Articles
    More By Jayesh Jain


     

    VB.NET ARTICLES

    - MyClass - Implementing Polymorphism in VB.Net
    - Building a News Ticker Using VB.Net
    - Everything You Wanted to Know About Forms In...
    - Building Assemblies with VB.Net
    - Simple VB.NET Notify Icon with Panel Applica...
    - Regular Expressions in .NET
    - String Encryption With Visual Basic .NET
    - Deploying Applications in VB.NET: Part 1/2
    - Watching Folder Activity in VB.NET
    - Creating A Windows Service in VB.NET
    - Implementing The Google Web Service In VB.NET
    - Migrating to Visual Basic.NET from Visual Ba...







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway