An extendable, simple and fluent file system watcher for .NET
The PollingWatcher
is the default file system watcher in WatchDog. It is a polling watcher that polls the directory every 100ms by default.
Create a new WatchDogHost
and add a PollingWatcher
WatchDogHost h = new WatchDogHost();
h.AddWatcher(new PollingWatcher(@"", 500));
h.Start();
Register a handler method for handeling changes that are detected by WatchDog
WatchDogHost h = new WatchDogHost();
h.AddWatcher(new PollingWatcher(@"", 500)
.UseChangeHandler(ChangeEventHandler));
h.Start();
Using the change handler method to do something
void ChangeEventHandler(IChangeSet changeSet)
{
if (changeSet.HasChanges())
{
foreach (ChangeItem c in changeSet.ChangeItems)
{
//do something
}
}
}
The easiest way to create a custom watcher is to take advantage of the default functionality by inheriting from AbstractWatcher
public class MyCustomWatcher : AbstractWatcher
{
public string Path { get; }
public Action<IChangeSet> OnChange { get; }
public bool Start() { }
public bool Stop() { }
}
If you would like to build your own watcher from the groud up then you can simply implement IWatcher
. This will still allow you to add it to your WatchDogHost
object.
public class MyCustomWatcher : IWatcher
{
public string Path { get; }
public Action<IChangeSet> OnChange { get; }
public bool Start() { }
public bool Stop() { }
}
WatchDog will not detect changes when watching a directory/file under the following conditions. Changing the watched directory/file to another state and returning that state back to its original state before the next poll interval. This will not fire the OnChange event.