检测使用FileSystemWatcher的移动的文件 [英] Detecting moved files using FileSystemWatcher

查看:198
本文介绍了检测使用FileSystemWatcher的移动的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道FileSystemWatcher对象不提供移动事件,相反它会生成一个单独的删除和创建相同的文件事件。 (FileSystemWatcher对象正在看源和目标文件夹)。

I realise that FileSystemWatcher does not provide a Move event, instead it will generate a separate Delete and Create events for the same file. (The FilesystemWatcher is watching both the source and destination folders).

不过,我们如何真正的文件移动和一些随机产生,恰好拥有相同的名称,这是最近删除的文件的文件的区分?

However how do we differentiate between a true file move and some random creation of a file that happens to have the same name as a file that was recently deleted?

某种的FileSystemEventArgs类的属性,如分配被删除的文件路径,如果它是一个移动或NULL的结果,否则AssociatedDeleteFile,将是巨大的。但是,当然,这并不存在。

Some sort of property of the FileSystemEventArgs class such as "AssociatedDeleteFile" that is assigned the deleted file path if it is the result of a move, or NULL otherwise, would be great. But of course this doesn't exist.

我也明白了FileSystemWatcher的是在文件系统的基本操作水平等概念的移动可能是唯一有意义的更高级别的应用程序。但是,如果是这样的话,人们会推荐什么样的算法来处理我的应用程序这种情况呢?

I also understand that the FileSystemWatcher is operating at the basic Filesystem level and so the concept of a "Move" may be only meaningful to higher level applications. But if this is the case, what sort of algorithm would people recommend to handle this situation in my application?

根据反馈更新:

FileSystemWatcher类似乎看到移动文件只需2个不同的事件,删除原始文件,随后创建在新的位置。

The FileSystemWatcher class seems to see moving a file as simply 2 distinct events, a Delete of the original file, followed by a Create at the new location.

不幸的是没有链接,这些事件之间,所以它并不明显如何将文件移动和正常删除或创建区分。在操作系统级别,此举是经过特殊处理的,可以移动说1GB的文件几乎在瞬间。

Unfortunately there is no "link" provided between these events, so it is not obvious how to differentiate between a file move and a normal Delete or Create. At the OS level, a move is treated specially, you can move say a 1GB file almost instantaneously.

一对夫妇的答案建议使用的文件哈希活动之间可靠地识别它们,我会proably采取这种做法。但是,如果有人知道如何更简单地检测一招,请留下一个答案。

A couple of answers suggested using a hash on files to identify them reliably between events, and I will proably take this approach. But if anyone knows how to detect a move more simply, please leave an answer.

推荐答案

按照文档

通用文件系统的操作可能
  提高多个事件。对于
  例如,当一个文件从一个移动
  目录到另一个,几
  并调用onChanged一些OnCreated和
  OnDeleted事件可能得到提升。
  移动一个文件是一个复杂的操作
  它由多个简单的
  操作,因此提高多
  事件。

Common file system operations might raise more than one event. For example, when a file is moved from one directory to another, several OnChanged and some OnCreated and OnDeleted events might be raised. Moving a file is a complex operation that consists of multiple simple operations, therefore raising multiple events.

所以,如果你想成为非常小心探测动作,并且具有相同的名字不够好,你将不得不使用某种启发的。例如,创建一个指纹使用文件名,大小,最后修改时间等在源文件夹中的文件。当你看到这可能预示此举无论如何,检查指纹对新的文件。

So if you're trying to be very careful about detecting moves, and having the same name is not good enough, you will have to use some sort of heuristic. For example, create a "fingerprint" using file name, size, last modified time, etc for files in the source folder. When you see any event that may signal a move, check the "fingerprint" against the new file.

这篇关于检测使用FileSystemWatcher的移动的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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