FileSystemWatcher

I'm right about to lose my mind...  so hopefully someone can help...

Here is the situation.  I wrote a program that has a file watcher on a folder on a clients machine.  When a document is dropped into the folder, the program runs and they enter information about the document and it's uploaded to their corporate web site.

I have tested this out locally, it all works fine.  We gave the installer to the client, they installed it and it worked fine.  We found a couple bugs so we needed to send a patch. They uninstalled the program, and then reinstalled.  But when they run the program and drop a document into the folder, the event doesn't run.  This is only happening there.  I have uninstalled and reinstalled many times on my machine and it works fine.

Here is the code for the file watcher:

Watcher = New FileSystemWatcher(LocalPath)
Watcher.NotifyFilter = (NotifyFilters.CreationTime Or NotifyFilters.FileName Or NotifyFilters.DirectoryName)

AddHandler Watcher.Created, AddressOf Watcher_Created

Watcher.EnableRaisingEvents = True

Mind you that this code works consistantly on my local machine, and did work before the reinstall at the client.  (This code was not modified in the patch).

We have checked user permissions, and pretty much anything else I could think of.





Answer this question

FileSystemWatcher

  • KYNg

    did the uninstaller remove all files

    there could have been some old files (if you had any) lurking around.

  • mhouston23

    also, are you sure that the user is doing dropping into the root path being watched. are you setting the IncludeSubdirectories property of the FSW to True, the default is False, beware.
  • AWJ

    #2,  you only need to be notified of changed, and sometimes renamed events. you need renamed if the user creates the file in the target path. when the user creates a file, several create events are fired, then a rename event is fired when the user gives the file a name other than the default "New Folder".

    In your case, if the user is simply dropping files then all you need is the Changed event.

    Also, what OS is your user using  the file system watcher does not work on all of microsoft's OS's, let alone other parties'....

  • Redpay

    i take what i said earlier back, you should watch for changed events to....see the following:



    Public MustInherit Class CHFileSystemWatcher
            Inherits FileSystemWatcher

            Protected Sub StartMonitoring()
                ' Define the event handlers.
                AddHandler Me.Changed, AddressOf OnChanged
                AddHandler Me.Created, AddressOf OnChanged
                AddHandler Me.Deleted, AddressOf OnChanged
                AddHandler Me.Renamed, AddressOf OnRenamed

                'the file watcher is started and runs on a thread pool thread automatically.
                Me.EnableRaisingEvents = True
            End Sub

            Protected MustOverride Overloads Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)

            Protected MustOverride Overloads Sub OnRenamed(ByVal source As Object, ByVal e As RenamedEventArgs)

        End Class




  • gmoffitt

    #1 if files is all you are interested in then why do you need to be notified of folders
  • KYC

    Oh yeah, one other thing....about the SynchronizingObject property of the FSW, this is an important property. Because a FSW, once started, runs on a thread pool thread, when it raises an event and your event handler catchs the event, the code you are executing is now running on the thread pool thread, UNLESS, you set the synchronizing object to say....your form....then, and only then, will your FSW event handler code now be running on the main thread. This is very important when it comes to updating controls on your main thread. I'm not sure how many, but some controls such as the TreeView control are not capable of being updated from a thread other than the thread they are running on. If you try to do that, you will have problems.

    Make sure that you are setting the SynchronizingObject property to your form.

    ex.
    FSWInstance.SynchronizingObject = Me (assuming that you are starting the FSW from Me)

  • FileSystemWatcher