filesystemwatcher中的问题 [英] Problem in filesystemwatcher

查看:58
本文介绍了filesystemwatcher中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi


描述:

我使用FileSystemWatcher在本地系统中唤醒文件夹(D:\ Watch)。如果zip文件夹到达该文件夹(D:\ Watch),则filesystemwatcher会引发创建的事件。最后拉链文件夹解压缩。



问题:

如果将80MB的Zip文件复制到文件夹中(D:\ Watch) 。在该位置创建zip文件需要大约1-2分钟的时间(D:\ Watch)。但是filesystemwatcher在完成复制之前引发创建的事件。所以我的extact功能失败了。



请帮帮我...



示例代码:< br $> b $ b

 命名空间 myproject 
{
public sealed class extractor
{

private FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();

public extractor()
{fileSystemWatcher.Path = @ D:\Watch;
fileSystemWatcher.Filter = * .zip :;
fileSystemWatcher.Created + = new FileSystemEventHandler(FileSystemWatcherCreated);
fileSystemWatcher.EnableRaisingEvents = true ;
}

private void FileSystemWatcherCreated( object sender,FileSystemEventArgs e)
{
Extract(e.fullPath);
}


}
}

解决方案

文档 [ ^ ] FileSystemWatcher类专门陈述您观察到的行为



公共文件系统操作可能会引发多个事件。例如,当文件从一个目录移动到另一个目录时,可能会引发几个OnChanged和一些OnCreated和OnDeleted事件。移动文件是一项复杂的操作,由多个简单操作组成,因此可以引发多个事件。同样,某些应用程序(例如,防病毒软件)可能会导致FileSystemWatcher检测到其他文件系统事件。



你可以检查一下已经释放了对文件的锁定,然后知道复制完成了吗?


这是一个很好的例子。



http://stackoverflow.com/questions/3822175/filesystem-watcher-and-large-files [ ^ ]



如果没有,你可以创建一个队列。每次观察者提出创建的事件时,您都可以将其放入队列中。然后,您可以创建一个计时器以轮询队列并稍后处理文件。这可以与IsLocked文件示例一起使用。


检查此工具:

智能观察者 [ ^ ]

hi
Description:
I used FileSystemWatcher to wach a folder (D:\Watch) in my local system. If a zip folder comes that folder (D:\Watch), the filesystemwatcher raise created event. Finally the zip folder is unzipped.

Problem:
if copy a 80MB of Zip file into the folder (D:\Watch). It takes some 1-2 min time to creating zip file in that location (D:\Watch). But the filesystemwatcher raise the created event before complete the copy. so that my extact function is failed.

Please help me...

sample code:

namespace myproject
{
    public sealed class extractor
    {

      private FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
      
      public extractor()
      {   fileSystemWatcher.Path = @"D:\Watch";
           fileSystemWatcher.Filter = "*.zip":;
           fileSystemWatcher.Created += new FileSystemEventHandler(FileSystemWatcherCreated);
           fileSystemWatcher.EnableRaisingEvents = true;
      }

     private void FileSystemWatcherCreated(object sender, FileSystemEventArgs e)
     {
       Extract(e.fullPath);
     }    


    }
}

解决方案

The documentation [^]for the FileSystemWatcher class specifically states your observed behaviour

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. Likewise, some applications (for example, antivirus software) might cause additional file system events that are detected by FileSystemWatcher.

You could check that a lock to the file has been released, and then know that the copying is complete?


an excellent example is this.

http://stackoverflow.com/questions/3822175/filesystem-watcher-and-large-files[^]

If not you could create a queue. Each time the watcher raise a created event you could put that into a queue. You could then create a timer to "Poll" the queue and process a file later. This could be used in conjunction with the "IsLocked" file example.


check this tool:
Smart Watcher[^]


这篇关于filesystemwatcher中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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