Keeping Track of the File System

Team-Fly    

 
Visual Basic .NET Unleashed
By Paul Kimmel
Table of Contents
Chapter 13.  Creating a Console Application


Some processes might require the presence or arrival of a file. For example, you might want to sort a file when it shows up on the machine running the sort process, or you might want to be notified when the sort has completed and the sorted file is available for further processing. Visual Basic .NET allows you to use the FileSystemWatcher component to dynamically track changes to the file system.

You can add a FileSystemWatcher to the component tray at the bottom of the form's designer or programmatically. Because FileSort is a console application, the component tray is not available. Therefore, we will add a FileSystemWatcher to the FileSort.sln programmatically and use it to notify us when the .tmp file containing the sorted data has been created (Listing 13.9 contains the new code added to the FileSort.sln sample program).

Listing 13.9 Programmatically adding a FileSystemWatcher component to a console application
  1:  #Region " FileSystemWatcher Example "  2:  Private Watcher As FileSystemWatcher  3:   4:  Private Sub OnCreated(ByVal sender As Object, _  5:  ByVal e As System.IO.FileSystemEventArgs)  6:  Console.WriteLine("Created: " & e.FullPath)  7:  End Sub  8:   9:  Private Sub AddWatcher()  10:  Watcher = New FileSystemWatcher("..\ ", "*.tmp")  11:  Watcher.EnableRaisingEvents = True  12:  AddHandler Watcher.Created, AddressOf OnCreated  13:  End Sub  14:  #End Region  15:   16:  Public Sub New(ByVal AFileName As String)  17:  FData = New ArrayList()  18:  FFileName = AFileName  19:  AddWatcher()  20:  End Sub 

The code region tagged " FileSystemWatcher Example " was added to the FileSort.sln sample program. The FileSystemWatcher is created in AddWatcher called by the New constructor on lines 16 to 20. Line 10 creates an instance of the FileSystemWatcher indicating the directory"..\" representing the executable's parent directoryto watch. The second parameter, "*.tmp", indicates that the watcher should watch changes to files in the executable's parent directory having a .tmp extension.

Line 11 indicates that the watcher should be raising events and line 12 establishes the OnCreated method as an event handler that will respond when .tmp files are created in the watcher's directory. The event handler is defined in lines 4 to 7; the watcher's event handler simply displays the full path of created files. Line 2 defines a reference variable named Watcher that will be used to reference the watcher component.

Whenever a .tmp file is created in the parent directory containing the executable, the watcher will display the complete path to that file. The FileSort.sln on this book's Web site contains the complete revised listing of the sample program.


Team-Fly    
Top
 


Visual BasicR. NET Unleashed
Visual BasicR. NET Unleashed
ISBN: N/A
EAN: N/A
Year: 2001
Pages: 222

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net