vb.net - FileSystemWatcher is not firing events -


as newbie in vb have troubles in filesystemwatcher. ominous reason not fire events. want check files being copied, deleted or edited in directory.

i appreciate help!!

here code:

public class filewatcher  public sub run(path string)     dim watcher new filesystemwatcher()      watcher.path = path     watcher.filter = "*.xml"     watcher.notifyfilter = notifyfilters.filename or notifyfilters.lastwrite or notifyfilters.creationtime      addhandler watcher.changed, addressof onchanged     addhandler watcher.created, addressof onchanged     addhandler watcher.deleted, addressof onchanged     addhandler watcher.renamed, addressof onrenamed      watcher.enableraisingevents = true  end sub   public function onchanged(source object, e filesystemeventargs) string     ' returns file name later use     console.writeline("monitoring: " + e.fullpath)     return e.fullpath end function  public function onrenamed(source object, e renamedeventargs) string     console.writeline("monitoring: " + e.fullpath)     return e.fullpath end function 

end class

the first thing consider here fact watcher local variable inside run method. means watcher garbage collected after exiting run method. need declare @ globlal class level , keep instance of class live until have finished use monitoring code.

public class filewatcher      dim watcher new filesystemwatcher()       public sub run(path string)          .... 

next keep in mind onchanged , onrenamed event handlers should declared sub not function. , of course don't return anything

if need value handlers need implement kind of global variables keeps data further processing. example list (the list of strings should expanded differentiate between added , deleted files or better use own class , list of classes instances)

dim changedfiles = new list(of string)() dim renamedfiles = new list(of string)() public sub onchanged(source object, e filesystemeventargs)      console.writeline("monitoring: " + e.fullpath)     changedfiles.add(e.fullpath) end sub  public sub onrenamed(source object, e renamedeventargs)     console.writeline("monitoring: " + e.fullpath)     renamedfiles.add(e.fullpath) end sub 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -