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
Properties
Methods
Events
Changed | This event is fired when the FileSystemWatcher detects a change in one of the watched directories. |
Constants
ItemAdded = &h1 | A 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 = &h2 | A 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 = &h4 | A 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 = &h20 | A 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)
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 bitmacOS Intel 64 bitmacOS Apple SiliconWindows 32 bitWindows 64 bitWindows ARM 64 bitLinux 32 bitLinux 64 bitLinux ARM 32 bitLinux ARM 64 bit