FileSystemWatcher Xojo Plugin

FileSystemWatcher Class (console safe)

FileSystemWatcher is a class for Xojo to set up file system watches on specific folder or folders.

This class can for example monitor when files are dropped into the folder or removed from the folder.

FileSystemWatcher was supposed to fully abstract the platforms and be seamless across them. This however was not possible since the File system monitoring API’s across platforms were very different with very different limitations.
For example then Linux does not support recursive watches, MacOS X does not support non recursive watches, and Windows supports both. Those are the kind of limitations we have to work with.

So there will be slight differences across platforms, and there might be and almost certainly will be other differences than described here above. We recommend testing you application on all platforms and work around what the platforms can and cannot do.


All of the operating systems in some way warn against putting a watch high up on the root or watch directories where you will have huge number of activity. And that the watches will not always give accurate results and there will be times where you need to do full directory scan to be sure.

Object
   FileSystemWatcher

class FileSystemWatcher

Constructors

FileSystemWatcherConstructor that takes no parameters.

Properties

FolderWatchCountReturns number of watched folders.
IsStartedReturns true if the FileSystemWatcher is started, else false.

Methods

AddFolderWatchAdds a folder to be watched.
FolderWatchAtRetrieves folder watch at a given index.
RemoveFolderWatchRemoves folder watch at a given index.
StartStarts watching the folders that have been added to the class instance.
StopStops watching the folders.

Events

ChangedThis event is fired when the FileSystemWatcher detects a change in one of the watched directories.

Constants

ItemAdded = &h1A bit mask constant that the changed event uses. This value defines that a Item was added, where the item can be either file or folder.
ItemRemoved = &h2A bit mask constant that the changed event uses. This value defines that a Item was removed, where the item can be either file or folder.
ItemModified = &h4A bit mask constant that the changed event uses. This value defines that a Item was modified in some way, where the item can be either file or folder.
ItemRenamed = &h20A bit mask constant that the changed event uses. This value defines that a Item was renamed, where the item can be either file or folder.

Examples

Its best to create a subclass that inherits from the FileSystemWatcher to grab the event from it. Or put the FileSystemWatcher on a window like you would put a timer on a window.

Simple example of inherited class that uses the event:

  • We create a class called MyFileSystemWatcher and make it inherit from FileSystemWather
  • Then we we let it implement the Changed event as shown bellow:


    List As ListBox

    Sub Constructor(list as Listbox)
        // Calling the overridden superclass constructor.
        Super.Constructor()
       
        me.List = list
    End Sub

    Sub Changed(f as FolderItem,changeFlags as Integer,oldFileName as String)
        list.AddRow(f.Name)
       
        Dim s as String
       
        if BitAnd(changeFlags, FileSystemWatcher.ItemAdded) = FileSystemWatcher.ItemAdded then
           list.Cell(list.LastIndex,1) = "Added"
          
        elseif BitAnd(changeFlags, FileSystemWatcher.ItemRemoved) = FileSystemWatcher.ItemRemoved then
           list.Cell(list.LastIndex,1) = "Removed"
          
        elseif BitAnd(changeFlags, FileSystemWatcher.ItemModified) = FileSystemWatcher.ItemModified then
           list.Cell(list.LastIndex,1) = "Modified"
          
        elseif BitAnd(changeFlags, FileSystemWatcher.ItemRenamed) = FileSystemWatcher.ItemRenamed then
           list.Cell(list.LastIndex,1) = "Renamed from: " + oldFileName
        end if
       
       
    End Sub



    Example use of the sub class:

    Dim f as FolderItem

    f = SelectFolder()

    if f <> nil then
        watcher = new MyFileSystemWatcher(Listbox1)
       
        watcher.AddFolderWatch(f)
        watcher.Start()
    end if



    Supported Platforms:

  • macOS Intel 32 bit
  • macOS Intel 64 bit
  • macOS Apple Silicon
  • Windows 32 bit
  • Windows 64 bit
  • Windows ARM 64 bit
  • Linux 32 bit
  • Linux 64 bit
  • Linux ARM 32 bit
  • Linux ARM 64 bit