不使用FilesystemWatcher的文件夹侦听器

我需要在C#中创建一个监听器来监视共享文件夹(UNC路径),并在到达时将具有特定扩展名(* .json)的文件复制到目标文件夹。 文件可以延迟大约半分钟。 该文件夹永远不会为空。

问题:

  1. 文件将到达新的子文件夹,FileSystemWatcher无法使用,因为它无法侦听共享文件夹中的子文件夹。

  2. 这些文件需要复制并保留在文件夹中,因此我们需要确保不会多次复制同一文件。

  3. 编辑/更新的文件需要再次复制并覆盖在目标文件夹中。

  4. 其他文件将在文件夹中,新文件将到达我们需要忽略的(没有正确的扩展名)。

我考虑过轮询文件夹,但我没有想出一个好的实现。

我很确定我不能使用FilesystemWatcher对象,但也许有人可以找到使用它的智能解决方案。

您的问题的一个解决方案可能是您可以持续检查位置一段时间并自己检查更改。

它不是一个完整的解决方案,而是一个需要考虑的想法。

  public async Task FindMyFile(string filePath) { int retries = 0; this.Founded = false; while (!this.Founded) { if (System.IO.File.Exists(filePath)) this.Founded = true; else if (retries < this.maxTries) { Console.WriteLine($"File {filePath} not found. Going to wait for 15 minutes"); await Task.Delay(new TimeSpan(0, 15, 0)); ++retries; } else { Console.WriteLine($"File {filePath} not found, retries exceeded."); break; } } }