【问题标题】:Tell fileSystemWatcher to wait告诉 fileSystemWatcher 等待
【发布时间】:2016-10-20 16:49:11
【问题描述】:

我有一些文件写入文件夹。首先写入 2 个文件,10-20 分钟后写入接下来的 2 个文件。

我的问题是:

有没有办法告诉文件系统观察者 在执行我的代码之前等到所有 4 个文件都在文件夹中?

【问题讨论】:

  • 创建一个类,并创建一个在所有 4 个条目都存在时触发的事件
  • 如果您知道要写入的最后一个文件的名称,则可以使用 Filter 属性仅等待与该文件相关的事件。

标签: c# file-watcher


【解决方案1】:

根据@BugFinder 的建议,我创建了类似但没有测试的东西。希望有用:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace CustomFileWatcher
{
    public class CustomFileWatcher : IDisposable
    {
        private FileSystemWatcher fileWatcher;
        private IList<string> fileList;
        private IList<string> createdFiles;

        public event EventHandler FilesCreated;
        protected void OnFilesCreated(EventArgs e)
        {
            var handler = FilesCreated;
            if (handler != null)
                handler(this, e);
        }

        public CustomFileWatcher(IList<string> waitForTheseFiles, string path)
        {
            fileList = waitForTheseFiles;
            createdFiles = new List<string>();
            fileWatcher = new FileSystemWatcher(path);
            fileWatcher.Created += fileWatcher_Created;
        }

        void fileWatcher_Created(object sender, FileSystemEventArgs e)
        {
            foreach (var item in fileList)
            {
                if (fileList.Contains(e.Name))
                {
                    if (!createdFiles.Contains(e.Name))
                    {
                        createdFiles.Add(e.Name);
                    }
                }
            }

            if (createdFiles.SequenceEqual(fileList))
                OnFilesCreated(new EventArgs());
        }

        public CustomFileWatcher(IList<string> waitForTheseFiles, string path, string filter)
        {
            fileList = waitForTheseFiles;
            createdFiles = new List<string>();
            fileWatcher = new FileSystemWatcher(path, filter);
            fileWatcher.Created += fileWatcher_Created;
        }

        public void Dispose()
        {
            if (fileWatcher != null)
                fileWatcher.Dispose();
        }
    }
}

用法

class Program
    {
        static void Main(string[] args)
        {
            IList<string> waitForAllTheseFilesToBeCopied = new List<string>();
            waitForAllTheseFilesToBeCopied.Add("File1.txt");
            waitForAllTheseFilesToBeCopied.Add("File2.txt");
            waitForAllTheseFilesToBeCopied.Add("File3.txt");

            string watchPath = @"C:\OutputFolder\";

            CustomFileWatcher customWatcher = new CustomFileWatcher(waitForAllTheseFilesToBeCopied, watchPath);

            customWatcher.FilesCreated += customWatcher_FilesCreated;
        }

        static void customWatcher_FilesCreated(object sender, EventArgs e)
        {
            // All files created.
        }
    }

【讨论】:

    猜你喜欢
    • 2010-11-08
    • 2020-07-29
    • 2022-01-02
    • 2010-12-21
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多