I am trying to monitor all files created/deleted/renamed in a folder using FileSystemWatcher in a docker volume. It is picking up events on the root dir (most of the time) that is being watched but nothing in subdir triggers an event even though IncludeSubdirectories = true. The watcher is saved as a prop. How can I get it to watch the subdir and trigger on all events not just most?
public void StartMonitorService()
{
Watcher = new FileSystemWatcher(@"/var/lib/docker/volumes/monitor");
Watcher.NotifyFilter = NotifyFilters.Attributes
| NotifyFilters.CreationTime
| NotifyFilters.DirectoryName
| NotifyFilters.FileName
| NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.Security
| NotifyFilters.Size;
Watcher.Created += OnCreated;
Watcher.Deleted += OnDeleted;
Watcher.Renamed += OnRenamed;
Watcher.Filter = "";
Watcher.IncludeSubdirectories = true;
Watcher.EnableRaisingEvents = true;
}
I didn't quite solve my question, but I have a work around for anyone who might have had the same issue I did.
private void OnCreated(object sender, FileSystemEventArgs e)
{
if (Directory.Exists(e.FullPath))
{
string dirRoot = e.FullPath;
DateTime lastTimeWriten = Directory.GetLastWriteTime(dirRoot);
while (true)
{
Thread.Sleep(30000);
if (lastTimeWriten == Directory.GetLastWriteTime(dirRoot))
break;
lastTimeWriten = Directory.GetLastWriteTime(dirRoot);
}
HashSet<string> fileSet = new HashSet<string>(Directory.GetFileSystemEntries(dirRoot, "*", SearchOption.AllDirectories));
...
_messageService.SendNewSurveyMessage(newSurveyFileSet);
}
}