首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >让fileSystemWatcher等着

让fileSystemWatcher等着
EN

Stack Overflow用户
提问于 2016-10-20 07:13:29
回答 1查看 480关注 0票数 0

我有一些文件被写入到一个folder.First,有2个文件被写入,在10-20分钟后,接下来的2个文件。

我的问题是:

有没有可能告诉文件系统观察者在执行我的代码之前等待文件夹中的所有4个文件?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-20 08:10:47

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

代码语言:javascript
复制
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();
        }
    }
}

使用

代码语言:javascript
复制
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.
        }
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40147780

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档