文件夹侦听器,无需使用FilesystemWatcher [英] Folder listener without using FilesystemWatcher

查看:147
本文介绍了文件夹侦听器,无需使用FilesystemWatcher的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to create a listener in C# that will watch a shared folder (UNC path) and copy the files with a specific extension (*.json) to a target folder when they arrive. The files can be delayed for about half a minute. The folder is never empty.

问题:

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

  1. The files will arrive in a new sub folder, FileSystemWatcher can't be used since it can't listen to sub folders in a shared folder.

需要复制文件并将其保留在文件夹中,因此我们需要确保同一文件不会被复制多次.

The files needs to be copied and left in the folder, so we need to assure that the same file isn't copied more than once.

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

Files that are edited/updated needs to be copied again and overwritten in the target folder.

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

Other files will be in the folder and new files will arrive that we need to ignore (doesn't have the right extension).

我考虑过轮询文件夹,但是我没有想到一个好的实现方法.

I thought about polling the folder , but I didn't come up with a good implementation.

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

I'm pretty sure I can't use the FilesystemWatcher object, but maybe someone can find a smart solution for using it.

推荐答案

您的问题的一种解决方案是,您可以在一段时间内不断检查位置并亲自检查更改.

One solution to your problem could be you can check the location constantly for a while and examine changes by yourself.

这不是一个完整的解决方案,而是一个值得考虑的想法.

it is not a complete solution, but an idea to consider.

    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;
            }
        }
    }

这篇关于文件夹侦听器,无需使用FilesystemWatcher的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆