FileSystemWatcher的问题 [英] FileSystemWatcher issues

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

问题描述

我试图用FileSystemWatcher对象 - 和我有一些运气。



我们的目标是将时生成的文件,从监视的文件夹,以一个新的文件夹。



不过......纷纷创出2障碍。首先,如果我移动3个文件放到一个文件夹一次(选择3个文件,CTRL + X,然后CTRL + C到我的监视文件夹),该显示器只触发为第一个文件。 ,另外2没有得到处理。



  FileSystemWatcher的FSW =新FileSystemWatcher的(FolderToMonitor); 
fsw.Created + =新FileSystemEventHandler(fsw_Created);

布尔显示器= TRUE;

,而(显示器)
{
fsw.WaitForChanged(WatcherChangeTypes.All,2000年);
如果(Console.KeyAvailable)
{
显示器= FALSE;
}
}

显示(用户已退出的过程中......,ConsoleColor.Yellow);
Console.ReadKey();



有没有办法让它看到所有3?



其次,如果我移动一个文件到监视文件夹,从不同的驱动器,它需要几秒钟就可以将文件复制到该文件夹​​。然而,监控,一旦触发作为文件开始复制进来。因此,因此,是只读的,并没有准备好移动。



有没有办法,我可以等待文件来完成它复制到监控文件夹,在我处理它?<​​/ p>

解决方案

据该的 MSDN文档




Windows操作系统通知
你的由
FileSystemWatcher的创建
缓冲文件更改组件。如果在很短的时间多
的变化,缓冲器
可以溢出。这会导致
成分失去跟踪在
中的目录变化的,它只会
提供毛毯通知。
增加与
上InternalBufferSize属性的缓冲区的大小为
昂贵的,因为它来自于一个不能被换出了
磁盘非分页
存储器,所以保持缓冲区小而
大到足以不会错过任何文件
变动事件。为了避免缓冲区
溢出,使用NotifyFilter和
IncludeSubdirectories属性,以便
你可以过滤掉不需要的更改
的通知。




也许这说明您的问题?结果
还要注意从一个目录到另一个剪切和粘贴文件基本上是一个单纯的重命名操作,因此,你应该使用<$ C 。$ C>改名事件来检测它们。



至于你的其他问题:请尝试使用更改事件连同创建,因为我相信双方会正好一次一个文件(注意,从另一个驱动器不是一个简单的重命名操作移动一个文件中提出:它的复制和删除),因此更改事件应说明当文件复制操作已经完成(即它不会火,直到文件完成)。


I am trying to use the FileSystemWatcher - and am having some luck..

The goal is to MOVE the file that gets created, from the monitored folder, to a new folder.

But... have hit 2 snags. Firstly, if I move 3 files into a folder at once (Select 3 files, ctrl+x, and then ctrl+c into my Monitor Folder), the monitor only triggers for the first file. The other 2 don't get processed.

            FileSystemWatcher fsw = new FileSystemWatcher(FolderToMonitor);
        fsw.Created += new FileSystemEventHandler(fsw_Created);

        bool monitor = true;

        while (monitor)
        {
            fsw.WaitForChanged(WatcherChangeTypes.All, 2000);
            if (Console.KeyAvailable)
            {
                monitor = false;
            }
        }

        Show("User has quit the process...", ConsoleColor.Yellow);
        Console.ReadKey();

Is there a way to make it see all 3?

Secondly, if I move a file into the monitor folder, from a different drive, it takes a few seconds to copy the file into the folder. However, the monitor triggers as soon as the file starts copying in.. so therefore, is read only, and not ready to be moved.

Is there a way I can wait for the file to complete it's copy into the monitor folder, before I process it?

解决方案

According to the msdn documentation:

The Windows operating system notifies your component of file changes in a buffer created by the FileSystemWatcher. If there are many changes in a short time, the buffer can overflow. This causes the component to lose track of changes in the directory, and it will only provide blanket notification. Increasing the size of the buffer with the InternalBufferSize property is expensive, as it comes from non-paged memory that cannot be swapped out to disk, so keep the buffer as small yet large enough to not miss any file change events. To avoid a buffer overflow, use the NotifyFilter and IncludeSubdirectories properties so you can filter out unwanted change notifications.

Perhaps that explains your issue?
Also note that cutting and pasting files from one directory to another is basically a mere renaming operation, therefore you should use the Renamed event to detect them.

As for your other problem: try using the Changed event together with Created, as I believe both will be raised exactly once for a file (note that moving a file from another drive in not a simple renaming operation: it's copy and delete), so the Changed event should indicate when the file copy operation has been completed (i.e. it won't fire until the file is complete).

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

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