469,945 Members | 2,361 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,945 developers. It's quick & easy.

FileSystemWatcher Event Does Not Fire

My VB.NET 2008 application is setup with a Sub Main and no forms. At run
time a NotifyIcon is created with one context menu choice (Close which
terminates app).

I have no trouble running the application and when I select Close from
the icon's right click menu the system tray icon goes away and the
application is removed from the listing on the processes tab of task
manager.

My problem is that the FileSystemWatcher event does not work. At the
moment since I have not written the file handling code yet the program
should display "Got here." in a message box whenever there is a change
to a file in C:\Temp, but that does not happen.

Here is the code with the notify and context menu event handlers omitted.
Code:
Public Module modMain
'define structures
Public Structure Configuration
Dim strDropFolder As String
Dim strTempFolder As String
End Structure

'define variables
Public conCfgInfo As Configuration
Public cmsNotify As System.Windows.Forms.ContextMenuStrip
Public fswTempFolder As System.IO.FileSystemWatcher
Public niNotify As System.Windows.Forms.NotifyIcon

Sub Main
cmsNotify = New System.Windows.Forms.ContextMenuStrip
fswTempFolder = New System.IO.FileSystemWatcher()
niNotify = New System.Windows.Forms.NotifyIcon

'load configuration data from Drop File Updater.cfg
Call LoadCfg() 'sub located in modMain and omitted

'set properties
'code for context menu and notifyicon properities omitted

fswTempFolder.Filter = "*.*"
fswTempFolder.NotifyFilter = IO.NotifyFilters.LastWrite
fswTempFolder.Path = conCfgInfo.strTempFolder
fswTempFolder.IncludeSubdirectories = False

'add event handlers
AddHandler fswTempFolder.Created, AddressOf fswNotify_Changed

'start monitoring
fswTempFolder.EnableRaisingEvents = True
Application.Run()
End Sub

Public Sub fswNotify_Changed(ByVal sender As System.Object, ByVal e _
As System.IO.FileSystemEventArgs)
'If e.ChangeType = IO.WatcherChangeTypes.Changed Then
MsgBox("Got here.")
'End If
End Sub
End Module

I am not sure if its relevant, but I find that I must have
Application.Run() as the last line in the Sub Main and
Application.Exit() in the event handler for the Close menu item after
niNotify.Dispose(). If I do not the system tray icon will disppear as
soon as I mouse over it, due to the application being no longer listed
in the processes tab of task manager.
Jun 27 '08 #1
1 2919
Is your FileSystemWatcher configured correctly? Setting the filter to LastWrite
and adding a handler for Created doesn't seem quite right to me.

I would suggest that you verify the configuration by creating a standard form
app as a test, and making sure that the file notification part works as expected
in that situation. Then you are not dealing with both the file notification and
the running-in-systray issues at the same time.
Lila Godel wrote:
My VB.NET 2008 application is setup with a Sub Main and no forms. At
run time a NotifyIcon is created with one context menu choice (Close
which terminates app).

I have no trouble running the application and when I select Close from
the icon's right click menu the system tray icon goes away and the
application is removed from the listing on the processes tab of task
manager.

My problem is that the FileSystemWatcher event does not work. At the
moment since I have not written the file handling code yet the program
should display "Got here." in a message box whenever there is a change
to a file in C:\Temp, but that does not happen.

Here is the code with the notify and context menu event handlers
omitted.

Code:
Public Module modMain
'define structures
Public Structure Configuration
Dim strDropFolder As String
Dim strTempFolder As String
End Structure

'define variables
Public conCfgInfo As Configuration
Public cmsNotify As System.Windows.Forms.ContextMenuStrip
Public fswTempFolder As System.IO.FileSystemWatcher
Public niNotify As System.Windows.Forms.NotifyIcon

Sub Main
cmsNotify = New System.Windows.Forms.ContextMenuStrip
fswTempFolder = New System.IO.FileSystemWatcher()
niNotify = New System.Windows.Forms.NotifyIcon

'load configuration data from Drop File Updater.cfg
Call LoadCfg() 'sub located in modMain and omitted

'set properties
'code for context menu and notifyicon properities omitted

fswTempFolder.Filter = "*.*"
fswTempFolder.NotifyFilter = IO.NotifyFilters.LastWrite
fswTempFolder.Path = conCfgInfo.strTempFolder
fswTempFolder.IncludeSubdirectories = False

'add event handlers
AddHandler fswTempFolder.Created, AddressOf fswNotify_Changed

'start monitoring
fswTempFolder.EnableRaisingEvents = True
Application.Run()
End Sub

Public Sub fswNotify_Changed(ByVal sender As System.Object, ByVal e _
As System.IO.FileSystemEventArgs)
'If e.ChangeType = IO.WatcherChangeTypes.Changed Then
MsgBox("Got here.")
'End If
End Sub
End Module

I am not sure if its relevant, but I find that I must have
Application.Run() as the last line in the Sub Main and
Application.Exit() in the event handler for the Close menu item after
niNotify.Dispose(). If I do not the system tray icon will disppear as
soon as I mouse over it, due to the application being no longer listed
in the processes tab of task manager.

Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Michael Stanbrook | last post: by
1 post views Thread by Troy Murphy | last post: by
7 posts views Thread by Allen Anderson | last post: by
3 posts views Thread by Stampede | last post: by
1 post views Thread by PadovaBoy | last post: by
reply views Thread by Silver Oak | last post: by
1 post views Thread by Abel Chan | last post: by
2 posts views Thread by Romain TAILLANDIER | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.