FileSystemWatcher的监视移动的文件 [英] FileSystemWatcher to monitor moved files

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

问题描述

可能重复:结果
检测移动使用FileSystemWatcher的

我试图文件显示器移动的文件与FileSystemWatcher的,并得到了来这儿的路上一些帮助:

I'm trying to monitor moved files with the FileSystemWatcher, and got some help on the way here:

使用FileSystemWatcher的多个文件

不过,我发现我必须能够同时使用删除,创建为了事件以获得从那里文件已经被移动以及路径它们已被移动,其中的路径。但是,当我为删除事件中添加类似的代码,我只能得到一个或其他事件来触发。它似乎是在那里我连线,确定哪些事件将运行事件的顺序。所以,如果我把创建活动最后在布线代码,将运行,反之亦然,如果我把删除接线最后,将运行,但没有创建。

However, I found I had to be able to use both the Deleted and the Created events in order to get the paths from where the files have been moved as well as the path to where they have been moved. But when I add similar code for the Delete event, I can only get either one or the other event to trigger. And it seems to be the order of where I wire the events that determines which event will run. So if I put the Created event last in the wiring code, that will run, and vice versa, if I put the Delete wiring last, that will run, but not Created.

下面的代码:

public class FileListEventArgs : EventArgs
{
    public List<string> FileList { get; set; }
}

public class Monitor
{
    private List<string> filePaths;
    private List<string> deletedFilePaths;
    private ReaderWriterLockSlim rwlock;
    private Timer processTimer;
    private Timer deletionTimer;
    public event EventHandler FileListCreated;
    public event EventHandler FileListDeleted;


    public void OnFileListCreated(FileListEventArgs e)
    {
        if (FileListCreated != null)
            FileListCreated(this, e);
    }

    public void OnFileListDeleted(FileListEventArgs e)
    {
        if (FileListDeleted != null)
            FileListDeleted(this, e);
    }

    public Monitor(string path)
    {
        filePaths = new List<string>();
        deletedFilePaths = new List<string>();

        rwlock = new ReaderWriterLockSlim();

        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Filter = "*.*";
        watcher.Deleted += new FileSystemEventHandler(watcher_Deleted);
        watcher.Created += watcher_FileCreated;


        watcher.Path = path;
        watcher.IncludeSubdirectories = true;
        watcher.EnableRaisingEvents = true;
    }


    private void ProcessQueue()
    {
        try
        {
            Console.WriteLine("Processing queue, " + filePaths.Count + " files created:");
            rwlock.EnterReadLock();

        }
        finally
        {
            if (processTimer != null)
            {
                processTimer.Stop();
                processTimer.Dispose();
                processTimer = null;
                OnFileListCreated(new FileListEventArgs { FileList = filePaths });
                filePaths.Clear();
            }
            rwlock.ExitReadLock();
        }
    }

    private void ProcessDeletionQueue()
    {
        try
        {
            Console.WriteLine("Processing queue, " + deletedFilePaths.Count + " files created:");
            rwlock.EnterReadLock();

        }
        finally
        {
            if (processTimer != null)
            {
                processTimer.Stop();
                processTimer.Dispose();
                processTimer = null;
                OnFileListDeleted(new FileListEventArgs { FileList = deletedFilePaths });

                deletedFilePaths.Clear();
            }
            rwlock.ExitReadLock();
        }
    }

    void watcher_FileCreated(object sender, FileSystemEventArgs e)
    {
        try
        {
            rwlock.EnterWriteLock();
            filePaths.Add(e.FullPath);

            if (processTimer == null)
            {
                // First file, start timer.
                processTimer = new Timer(2000);
                processTimer.Elapsed += (o, ee) => ProcessQueue();
                processTimer.Start();
            }
            else
            {
                // Subsequent file, reset timer. 
                processTimer.Stop();
                processTimer.Start();
            }

        }
        finally
        {
            rwlock.ExitWriteLock();
        }
    }

    void watcher_Deleted(object sender, FileSystemEventArgs e)
    {
        try
        {
            rwlock.EnterWriteLock();
            deletedFilePaths.Add(e.FullPath);

            if (deletionTimer == null)
            {
                // First file, start timer.
                deletionTimer = new Timer(2000);
                deletionTimer.Elapsed += (o, ee) => ProcessDeletionQueue();
                deletionTimer.Start();
            }
            else
            {
                // Subsequent file, reset timer. 
                deletionTimer.Stop();
                deletionTimer.Start();
            }

        }
        finally
        {
            rwlock.ExitWriteLock();
        }
    }



那么,如何做到这一点,以获得原始路径文件是在哪,以及它们已被移动,其中的新路径? (请参阅为什么计时器的代码的第一个问题是存在的,以拖延,直到所有的文件已在多文件移动被移动的事件的处理)。

So how do I do this to get the original path where the files were, as well as the new path to where they have been moved? (See the first question for why the timer code is there in order to hold off the handling of the events until all files have been moved in a multifile move).

推荐答案

您声明两个定时器,但你只使用其中一个(你在使用过程中删除队列方法相同)。所以看起来像一个简单的复制/粘贴错误的开始。

You declare two timers, but you only use one of them (you use the same one in the process deletion queue method). So looks like a simple copy/paste mistake to begin with.

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

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