I have a real dilema here. A simple FileSystemWatcher snippet that will work as long as I'm watching the local C: drive at my office but it will not work when I'm trying to monitor folders on the Novell servers in this corporate LAN. So I tested the same code on my LAN at home which is BSD server running Samba. The Windows clients in my home LAN see the BSD server as though it were a Windows server and I have drives mapped, etc... On my home LAN the code works perfectly in that I can monitor a folder on the server whether I call it by it's mapped drive name or UNC format. So the code appears to be solid. But here at this office no matter which way I try it the FileSystemWatcher program just sits there looking dumb when I add a new file to the folder on the server being monitored. The IT guys here are not much help and have told me it should work.
Here is the VB.NET code snippet. It runs with a small form which has three buttons and a multi-line textbox. I have found that FileSystemWatcher seems to only function fully once you have compiled and run the exe. Just starting Debug will get work but when a file is added to the monitored folder the debugging process just stops. At least I can confirm that something did happen and when I run the exe file I can see that the code works as planned.
Imports System.IO
Imports System.Diagnostics
Public Class Form1
Public watcher As New FileSystemWatcher
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
watcher.NotifyFilter = (NotifyFilters.FileName)
' Only watch text files.
watcher.Filter = "*.txt"
' Try with UNC format and drive mapping
' watcher.Path = "\\a_very_long_novell_unc_path\My_Folder"
watcher.Path = "I:\My_Folder"
AddHandler watcher.Created, AddressOf OnChanged
' Begin watching the folder
watcher.EnableRaisingEvents = True
btnStop.Enabled = True
btnStart.Enabled = False
txtDwgLog.Text = txtDwgLog.Text & "Monitoring started at " & (Now.ToString("f")) & vbCrLf
End Sub
Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)
txtDwgLog.Text = txtDwgLog.Text & (e.FullPath) & " added to folder." & vbCrLf
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
watcher.EnableRaisingEvents = False
btnStop.Enabled = False
btnStart.Enabled = True
txtDwgLog.Text = txtDwgLog.Text & "Monitoring stopped at " & (Now.ToString("f")) & vbCrLf
End Sub
End Class