FileSystemWatcher won't fire for directory events

  • Thread starter Thread starter Mike Beebe
  • Start date Start date
M

Mike Beebe

I trying to be notified when a directory is created, deleted, renamed or
moved. What am I doing wrong?

moFolderSysWatch = New FileSystemWatcher(m_WatchDirectory, "")
moFolderSysWatch.IncludeSubdirectories = True
moFolderSysWatch.NotifyFilter = NotifyFilters.DirectoryName
moFolderSysWatch.EnableRaisingEvents = True
 
Am 05.03.2010 19:13, schrieb Mike Beebe:
I trying to be notified when a directory is created, deleted, renamed or
moved. What am I doing wrong?

moFolderSysWatch = New FileSystemWatcher(m_WatchDirectory, "")
moFolderSysWatch.IncludeSubdirectories = True
moFolderSysWatch.NotifyFilter = NotifyFilters.DirectoryName
moFolderSysWatch.EnableRaisingEvents = True

So, are you sure you are setting up the event handlers, either by making
'moFolderSysWatch' and 'WithEvents' variable and using 'Handles' at the
event handler procedures, or by adding the handler using the
'AddHandler' statement?
 
Here's my code (snippets):

Private WithEvents moFolderSysWatch As FileSystemWatcher

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
moFolderSysWatch = New FileSystemWatcher(m_WatchDirectory, "")
moFolderSysWatch.IncludeSubdirectories = True
moFolderSysWatch.NotifyFilter = NotifyFilters.DirectoryName
moFolderSysWatch.EnableRaisingEvents = True
End Sub

Private Sub moFolderSysWatch_Renamed(ByVal sender As Object, ByVal e As
System.IO.RenamedEventArgs) Handles moFolderSysWatch.Renamed
Debug.Print("moFolderSysWatch_Renamed")
RenamedFolder(e)
End Sub

Private Sub RenamedFolder(ByVal e As System.IO.RenamedEventArgs)
'Needed for cross-thread calls
If Me.InvokeRequired Then 'are we running on a secondary thread
Dim d As New RenamedFolderDelegate(AddressOf RenamedFileHandler)
Me.Invoke(d, New Object() {e})
Else
RenamedFolderHandler(e)
End If
End Sub

'Needed for cross-thread calls
Private Delegate Sub RenamedFolderDelegate(ByVal e As
System.IO.RenamedEventArgs)

Private Sub RenamedFolderHandler(ByVal e As System.IO.RenamedEventArgs)
lstFiles.Items.Add(Now.ToString() & " Renamed '" & e.OldFullPath & _
"' to '" & e.FullPath & "'")
End Sub
 
Sorry, that should have been RenamedFolderHandler. The corrected code is
below.


Imports System.IO
Imports System.Threading

Public Class Form1
Inherits System.Windows.Forms.Form


#Region " Windows Form Designer generated code "
#End Region

Private m_WatchDirectory As String
Private WithEvents moFolderSysWatch As FileSystemWatcher

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

m_WatchDirectory = Application.StartupPath
m_WatchDirectory = m_WatchDirectory.Substring(0, _
m_WatchDirectory.LastIndexOf("\"))
m_WatchDirectory &= "\Files"

moFolderSysWatch = New FileSystemWatcher(m_WatchDirectory, "")
moFolderSysWatch.IncludeSubdirectories = True
moFolderSysWatch.NotifyFilter = NotifyFilters.DirectoryName
moFolderSysWatch.EnableRaisingEvents = True
End Sub

Private Sub moFolderSysWatch_Renamed(ByVal sender As Object, ByVal e As
System.IO.RenamedEventArgs) Handles moFolderSysWatch.Renamed
Debug.Print("moFolderSysWatch_Renamed")
RenamedFolder(e)
End Sub

Private Sub RenamedFolder(ByVal e As System.IO.RenamedEventArgs)
'Needed for cross-thread calls
If Me.InvokeRequired Then 'are we running on a secondary thread
Dim d As New RenamedFolderDelegate(AddressOf RenamedFolderHandler)
Me.Invoke(d, New Object() {e})
Else
RenamedFolderHandler(e)
End If
End Sub

'Needed for cross-thread calls
Private Delegate Sub RenamedFolderDelegate(ByVal e As
System.IO.RenamedEventArgs)

Private Sub RenamedFolderHandler(ByVal e As System.IO.RenamedEventArgs)
lstFiles.Items.Add(Now.ToString() & " Renamed '" & e.OldFullPath & _
"' to '" & e.FullPath & "'")
End Sub

End Class
 
Am 09.03.2010 17:09, schrieb Mike Beebe:
moFolderSysWatch = New FileSystemWatcher(m_WatchDirectory, "")

Drop the 2nd arg. No file matches "". Dropping it means "*.*" which fits all.

And, if you want to monitor changes of a directory name, you must monitor
it's _parent_ folder.
m_WatchDirectory = m_WatchDirectory.Substring(0, _
m_WatchDirectory.LastIndexOf("\"))
m_WatchDirectory &= "\Files"

IO.Path.Get* and IO.Path.Combine are helpful.
 
Changing the following made it work. BTW, OnRenamed and
moFileSysWatch_Renamed both execute.

moFolderSysWatch = New System.IO.FileSystemWatcher()


moFolderSysWatch = New System.IO.FileSystemWatcher()
moFolderSysWatch.Path = m_WatchDirectory
moFolderSysWatch.IncludeSubdirectories = True
'moFolderSysWatch.NotifyFilter = NotifyFilters.DirectoryName
moFolderSysWatch.NotifyFilter = IO.NotifyFilters.DirectoryName
AddHandler moFolderSysWatch.Renamed, AddressOf OnRenamed
moFolderSysWatch.EnableRaisingEvents = True
 
Armin Zingler said:
Am 09.03.2010 17:09, schrieb Mike Beebe:

Drop the 2nd arg. No file matches "". Dropping it means "*.*" which fits all.

And, if you want to monitor changes of a directory name, you must monitor
it's _parent_ folder.


IO.Path.Get* and IO.Path.Combine are helpful.
 
Thanks Armin.

How can I tell if e.FullPath is a file or folder? File do NOT have to have
a file extension.
 
Am 10.03.2010 18:17, schrieb Mike Beebe:
Thanks Armin.

How can I tell if e.FullPath is a file or folder? File do NOT have to have
a file extension.

Right, and directories can have an extension.


Dim IsDirectory = IO.Directory.Exists(e.FullPath)
 
Perfect! Thank you sir.

Armin Zingler said:
Am 10.03.2010 18:17, schrieb Mike Beebe:

Right, and directories can have an extension.


Dim IsDirectory = IO.Directory.Exists(e.FullPath)
 
Back
Top