Filesystemwatcher 不监视子目录
Filesystemwatcher is not watching subdir
我正在尝试使用 docker 卷中的 FileSystemWatcher 监视文件夹中的所有文件 created/deleted/renamed。它正在获取正在监视的根目录(大部分时间)上的事件,但即使 IncludeSubdirectories = true,subdir 中也没有任何事件触发事件。观察者被保存为道具。我怎样才能让它观看子目录并触发所有事件,而不仅仅是大多数事件?
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;
}
我没有完全解决我的问题,但对于可能遇到与我相同的问题的任何人,我都有解决办法。
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);
}
}
我正在尝试使用 docker 卷中的 FileSystemWatcher 监视文件夹中的所有文件 created/deleted/renamed。它正在获取正在监视的根目录(大部分时间)上的事件,但即使 IncludeSubdirectories = true,subdir 中也没有任何事件触发事件。观察者被保存为道具。我怎样才能让它观看子目录并触发所有事件,而不仅仅是大多数事件?
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;
}
我没有完全解决我的问题,但对于可能遇到与我相同的问题的任何人,我都有解决办法。
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);
}
}