C# 文件系统监视程序还是目录读取?

C# 文件系统监视程序还是目录读取?,c#,architecture,windows-services,C#,Architecture,Windows Services,Conext 我有一个场景,在这个场景中,我必须每30秒检查一次特定的目录以查找新文件。如果有任何新的文件,那么我必须处理这些文件,但可以在一批多个 问题 我应该使用FileSystemWatcher还是读取目录并并行处理文件 我正在使用Windows服务,它将处理CSV文件,并将输出显示到Windows窗体应用程序 我们能按时安排FileSystemWatcher吗 在这种情况下,最好的方法是什么 如果我选择目录读取而不是FileSystemWatcher,如何并行处理这批100个文件并发送

Conext

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

问题

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

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

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

谢谢

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

和队列处理程序:

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吗,因为我必须批量收集文件。FileSystem watcher可以与计时器一起用于调度。或者,如果您想在特定时间使用Windows TaskScheduler,也可以使用Windows TaskScheduler。我使用FileSystem watcher是因为我想要即时结果。
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()));    
    }