views:

81

answers:

2

I added FileSystemWatcher in Form1_Load like this -

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ....................
        Dim watcher As New FileSystemWatcher()
        'For watching current directory
        watcher.Path = "/"
        'For watching status.txt for any changes
        watcher.Filter = "status.txt"
        watcher.NotifyFilter = NotifyFilters.LastWrite
        watcher.EnableRaisingEvents = True
        AddHandler watcher.Changed, AddressOf OnChanged
End Sub

And my OnChanged function is simply a MessageBox. Yet, When I change the status.txt, No message box is shown.

A: 

You should also listen for the Deleted event.

Depending on the editor you're using, they sometimes Delete/Replace the file instead of simply changing it.

Justin Niessner
Not Working yet!
Shubham
+1  A: 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim watcher As New IO.FileSystemWatcher()

'For watching current directory
watcher.Path = **System.IO.Directory.GetCurrentDirectory()** 'Note how to obtain current directory
watcher.NotifyFilter = NotifyFilters.LastWrite

'When I pasted your code and created my own status.txt file using 
'right click->new->Text File on Windows 7 it appended a '.txt' automatically so the
'filter wasn't finding it as the file name was status.txt.txt renaming the file
'solved the problem
watcher.Filter = "status.txt" 

AddHandler watcher.Changed, AddressOf OnChanged

watcher.EnableRaisingEvents = True
End Sub

Private Shared Sub OnChanged(ByVal source As Object, ByVal e As IO.FileSystemEventArgs)
MessageBox.Show("Got it")
End Sub

From http://bartdesmet.net/blogs/bart/archive/2004/10/21/447.aspx

You may notice in certain situations that a single creation event generates multiple Created events that are handled by your component. For example, if you use a FileSystemWatcher component to monitor the creation of new files in a directory, and then test it by using Notepad to create a file, you may see two Created events generated even though only a single file was created. This is because Notepad performs multiple file system actions during the writing process. Notepad writes to the disk in batches that create the content of the file and then the file attributes. Other applications may perform in the same manner. Because FileSystemWatcher monitors the operating system activities, all events that these applications fire will be picked up

Kefala
Thanks it is working. Just one more problem. The alert is being showed two times for one change.
Shubham