FileSystemWatcher不会使用'mv'命令检测移动到目录中的文件,也不会在ubuntu上剪切和粘贴
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FileSystemWatcher不会使用'mv'命令检测移动到目录中的文件,也不会在ubuntu上剪切和粘贴相关的知识,希望对你有一定的参考价值。
我正在使用FileSystemWatcher来检测放入目录的文件,然后将它们转发到别处并从目录中删除。在开发期间,我只是将文件复制到目录中进行测试,但是我只是意识到没有事件在qa中被触发,因为文件被移动,而不是复制到目录中。有没有办法在ubuntu上使用c#dotnet core捕获移动到目录中的文件?
下面是我的代码的摘录,显示我如何设置观察者,在我的实际应用程序中,我只是观看已更改的事件,但添加了更多事件监听器以尝试找出如何执行此操作
CaptureServerSettings appSettings = provider.GetRequiredService<CaptureServerSettings>();
using (FileSystemWatcher directoryWatcher = new FileSystemWatcher(appSettings.WatchDirectory, "*.*"))
{
directoryWatcher.BeginInit();
directoryWatcher.IncludeSubdirectories = false;
directoryWatcher.NotifyFilter = NotifyFilters.LastWrite;
directoryWatcher.Error += errorEvent;
directoryWatcher.Renamed += renameEvent;
directoryWatcher.Created += createdEvent;
directoryWatcher.Changed += changedEvent;
directoryWatcher.EnableRaisingEvents = true;
directoryWatcher.EndInit();
provider.GetRequiredService<CancellationTokenSource>().Token.WaitHandle.WaitOne();
}
private static void changedEvent(object sender, FileSystemEventArgs e) => _logger.LogWarning("Changed: {@eventargs}", e);
private static void errorEvent(object sender, ErrorEventArgs e) => _logger.LogWarning("Error: {@eventargs}", e);
private static void createdEvent(object sender, FileSystemEventArgs e) => _logger.LogWarning("Created: {@eventargs}", e);
private static void renameEvent(object sender, RenamedEventArgs e) => _logger.LogWarning("Renamed: {@eventargs}", e);
答案
本应该想出这个而不发布到堆栈......我的问题是两件事。我需要更改NotifyFilter以包含FileName更改并订阅已更改的事件,如下所示。
CaptureServerSettings appSettings = provider.GetRequiredService<CaptureServerSettings>();
using (FileSystemWatcher directoryWatcher = new FileSystemWatcher(appSettings.WatchDirectory, "*.*"))
{
directoryWatcher.BeginInit();
directoryWatcher.IncludeSubdirectories = false;
directoryWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName;
directoryWatcher.Created += createdEvent;
directoryWatcher.Changed += changedEvent;
directoryWatcher.EnableRaisingEvents = true;
directoryWatcher.EndInit();
provider.GetRequiredService<CancellationTokenSource>().Token.WaitHandle.WaitOne();
}
以上是关于FileSystemWatcher不会使用'mv'命令检测移动到目录中的文件,也不会在ubuntu上剪切和粘贴的主要内容,如果未能解决你的问题,请参考以下文章