4

I want to detect every filechanges on a specific folder (except data changes). I decided to use System.IO.FileSystemWatcher to manage that.

// 
// fileSysWatchFile
// 
this.fileSysWatchFile.EnableRaisingEvents = true;
this.fileSysWatchFile.IncludeSubdirectories = true;
this.fileSysWatchFile.NotifyFilter = System.IO.NotifyFilters.FileName;
this.fileSysWatchFile.SynchronizingObject = this;
this.fileSysWatchFile.Created += new System.IO.FileSystemEventHandler(this.fileSysWatchFile_Created);
this.fileSysWatchFile.Deleted += new System.IO.FileSystemEventHandler(this.fileSysWatchFile_Deleted);
this.fileSysWatchFile.Renamed += new System.IO.RenamedEventHandler(this.fileSysWatchFile_Renamed);

As far as good... New files are detected. File deletes are detected. File renames are detected.

When I move a file to a subfolder it detects first a file delete and then a new file create.

I'd expect that a move is the same as a rename except the path. Seems that it isn't. Can I detect file moves in a save way?

By the way... I only want to detect file changes and not directory changes.

Edit:

Additional Info why I have to detect moves and can't live with delete, create:

I want to replay the same changes on an other drive. If I get a delete first, I delete the shadow file. Then I get the create file event and the original file is already lost :-(.

So I have a drive A which is the watched drive... And a drive B which has files with the same filenames.

All file changes exept data changes should be replayed on drive B.

4

3 回答 3

1

您可以使用文件系统过滤驱动程序来跟踪文件重命名操作。实际上,FS Filter 是比 FileSystemWatcher 更好的方法。FileSystemWatcher 在某些情况下不提供可靠性和灵活性(您可以看到有关 FileSystemWatcher 的问题数量以及它的故障和限制)。

FS 过滤器可让您在请求到达文件系统后立即对其进行跟踪。

您可以编写自己的过滤器驱动程序,或使用我们的CallbackFilter产品。

于 2010-11-27T11:34:41.133 回答
1

文件删除/文件创建功能是文件移动的背后。如果您只是将文件从文件夹移动到文件夹,这类似于重命名,但是如果将文件从一个磁盘移动到另一个磁盘,或者在机器之间移动文件呢?

而且,如果我正在观看指定的文件夹,只要该文件不存在,它还可能已被删除:)

如果您确定要捕获文件移动的“事件”(从监视文件夹到监视子文件夹),我会维护一个最近删除文件的列表,并在每个文件创建事件时,检查文件是否是该列表,表示事实上的文件移动。

于 2010-11-27T09:17:06.590 回答
0

这不是问题的真正解决方案,但我设法有一个快速而肮脏的解决方案:

首先,我将所有事件缓冲一段时间(用 100 毫秒进行测试,但我们将看到它的速度有多快)。

如果缓冲区中的事件为 100 毫秒,我会检查其中是否还有其他相关事件。因此,对于删除,我搜索所有创建,对于创建,我搜索所有删除。

如果我找到一个,我只用一个移动事件替换这两个事件。

这个workarround有一些风险:
1.)我不能说什么是第一位的,删除或创建。似乎这是每次都不同
2.) 如果延迟时间很短,则文件将被删除并丢失:-(

但只要我没有更好的解决方案,我就必须忍受这个。

于 2010-11-27T15:35:04.597 回答