FileSystemWatcher用于监视移动的文件

时间:2011-08-05 00:12:24

标签: c# filesystemwatcher

  

可能重复:
  Detecting moved files using FileSystemWatcher

我正在尝试使用FileSystemWatcher监视已移动的文件,并在此处获得了一些帮助:

Using FileSystemWatcher with multiple files

但是,我发现我必须能够同时使用Deleted和Created事件来获取文件移动的路径以及移动文件的路径。但是当我为Delete事件添加类似的代码时,我只能触发一个或另一个事件。它似乎是我连接事件的顺序,决定了哪个事件将运行。因此,如果我将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();
        }
    }

那么我该怎么做才能获得文件所在的原始路径,以及它们被移动到的新路径? (请参阅第一个问题,了解为什么定时器代码存在,以便在多文件移动中移动所有文件之前推迟处理事件。)

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

我发现FileSystemWatcher事件没有排队(警告:这是大约8年前)。

如果您正在处理文件,并且在处理过程中创建了3个新文件,则不会获得3个事件。

处理完文件后,您可能需要手动检查目录是否有其他更改。