C#:监视复制或移动文件,FileSystemWatcher的 [英] C#: Monitoring copied or moved files with FileSystemWatcher

查看:638
本文介绍了C#:监视复制或移动文件,FileSystemWatcher的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所遇到的几个解决方案,在这里和整个网络,但没有一个似乎符合这个要求。

I have come across several "solutions" here and across the web but none seem to fit the bill.

我所希望做的是有一个应用程序监视的文件夹为新文件(通过创建,移动,或复印件),并执行对这些对象的动作。这是该场景中,我转向了FileSystemWatcher类执行此操作。

What I am looking to do is have an app monitor a folder for new files (either by creation, a move, or a copy) and perform actions on those objects. That being the scenario, I turned to the FileSystemWatcher class to perform this action.

现在的问题是,整个文件被创建之前(最明显的通过一个大文件的副本所示)文件FileSystemWatcher.Created事件。

The problem is that the file FileSystemWatcher.Created event is fired before the entire file is created (most noticeably seen through a copy of a large file).

有没有办法让这个事件火灾文件创建的结束,而不是开始?我尝试了各种组合的的FileSystemWatcher.NofityFilter财产,但没有成功。

Is there any way to have this event fire at the conclusion of the file creation as opposed to the beginning? I have tried various combination's of the FileSystemWatcher.NofityFilter property with no success.

在此先感谢! :)

推荐答案

我已经使用了几个解决方案的这种情况。

I have used a couple of solutions for this situation.

  1. 如果您可以用文件的创建者,并使用一个重命名计划的文件。例如。创建该文件的 _ 名称同时创建,并在过程结束时将其重命名为名称和事件将触发,你有一个完整的文件。

  1. If you can work with the creator of the file and use a renaming scheme for the file. EG. Create the File as _Name while being created and at the end of the process rename it to Name and the event will fire and you have a complete file.

当您的触发器触发检查如果你能得到的文件的独占只读锁。如果可以则写操作已经完成到文件中。 (我在另一个问题写了一些关于这个<一href="http://stackoverflow.com/questions/3167647/keep-settings-in-sync-between-forms-application-and-windows-service-or-any-n-tie/3278034#3278034">http://stackoverflow.com/questions/3167647/keep-settings-in-sync-between-forms-application-and-windows-service-or-any-n-tie/3278034#3278034)

When your trigger fires check if you can get an exclusive readonly lock on the file. If you can then the write operation has been completed to the file. (I wrote something about this in another question http://stackoverflow.com/questions/3167647/keep-settings-in-sync-between-forms-application-and-windows-service-or-any-n-tie/3278034#3278034)

你可能集成类似#2到更改事件,然后你会得到的结果。

You could possibly integrate something like #2 into your Changed Event and then you'll get the result.

这篇关于C#:监视复制或移动文件,FileSystemWatcher的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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