Is your FileSystemWatcher configured correctly? Setting the filter to LastWrite
and adding a handler for Created doesn't seem quite right to me.
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.
Quote:
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.