【问题标题】:FileSystemWatcher or Directory Read?FileSystemWatcher 或目录读取?
【发布时间】:2015-12-03 21:58:57
【问题描述】:

上下文

我有一个场景,我必须每 30 秒检查一次特定目录是否有新文件。如果有任何新文件,那么我必须处理这些文件,但可以批量处理多个文件。

问题

我应该使用 FileSystemWatcher 还是读取目录并并行处理文件?

我正在使用 Windows 服务,它将处理 CSV 文件并将输出显示到 Windows 窗体应用程序。

  • 我们可以在 Timer 上安排 FileSystemWatcher 吗?
  • 在这种情况下最好的方法是什么?
  • 如果我选择目录读取而不是 FileSystemWatcher 如何并行处理这批 100 个文件并发送到其他应用程序?

谢谢

【问题讨论】:

    标签: c# architecture windows-services


    【解决方案1】:

    我也在尝试,发现这篇文章很有趣。

    https://connectvishal.wordpress.com/2015/11/05/filesystemwatcher-and-queues-with-parallel-execution/

    protected override void OnStart(string[] args)    
    {    
    
        current_directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);    
    
        try   
        {    
            strDir = ConfigurationManager.AppSettings["Directory"];    
            fileMask = ConfigurationManager.AppSettings["FileMask"];    
            strBatfile = ConfigurationManager.AppSettings["Batch"];    
            strlog = ConfigurationManager.AppSettings["Log"];    
    
            Task.Factory.StartNew(QueueHandler);    
    
            var fsw = new FileSystemWatcher();    
            fsw.Created += (o, e) =>    
            {    
                // add a file to the queue    
                filenames.Enqueue(e.FullPath);    
    
            };    
    
            fsw.Path = strDir + "\\";    
            fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite    
                             | NotifyFilters.FileName | NotifyFilters.DirectoryName;    
            fsw.Filter = fileMask;    
    
            fsw.EnableRaisingEvents = true;    
            fsw.Deleted += new FileSystemEventHandler(OnDeleated);    
            fsw.Renamed += new RenamedEventHandler(OnRenamed);    
    
    
            fsw.EnableRaisingEvents = true;    
        }    
        catch (Exception exception)    
        {    
            CustomException.Write(CustomException.CreateExceptionString(exception.ToString()));    
        }    
    
    } 
    

    还有一个队列处理程序:

    static void QueueHandler()    
    {    
        bool run = true;    
        AppDomain.CurrentDomain.DomainUnload += (s, e) =>    
        {    
            run = false;    
            filenames.Enqueue("stop");    
        };    
        try   
        {    
            while (run)    
            {    
                string filename;    
                if (filenames.TryDequeue(out filename) && run)    
                {    
                    var proc = new Process();    
                    proc.StartInfo.FileName = Service1.strBatfile;   //here .exe can be added  
                    proc.Start();    
                   ;    
                    Log.getLogger("File Processed after executing batch\.exe:  Filename - :" + filename + " " + "Batch File Executed- > " + Service1.strBatfile + " at timestamp : " + DateTime.Now.ToString(), Service1.strlog);    
                    proc.WaitForExit(); // this blocks until the process ends....    
    
                }    
            }    
        }    
        catch (Exception exception)    
        {    
            CustomException.Write(CustomException.CreateExceptionString(exception.ToString()));    
        } 
    

    【讨论】:

    • @wOxxOm 改变了我的答案
    • 谢谢 Vish,但我可以使用 FileSystemWatcher 进行调度吗,因为我必须批量收集文件。
    • 文件系统观察器可以与定时器一起使用来进行调度。或者如果你想在特定时间使用 Windows TaskScheduler。我使用了文件系统观察器,因为我想要即时结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-08
    • 2013-02-07
    • 2021-10-11
    • 1970-01-01
    • 2011-01-29
    相关资源
    最近更新 更多