[英]Monitor folder with FileSystemWatcher fails after first time
Many times a day I receive xml-files from different users. 每天我常常收到来自不同用户的xml文件。 These are FTPed to a folder on my drive, say D:\\batch. 这些文件通过FTP传输到我的驱动器上的文件夹,例如D:\\ batch。 As today I check these files with a Scheduled Task which starts an ASP.NET page and processes the files found and answers the uploader and ends. 今天,我使用计划任务检查这些文件,该任务将启动ASP.NET页并处理找到的文件并回答上传器并结束。 This task is run every 15 minutes, but the uploaders wants the answers quicker, so I'm trying to accomplish this. 该任务每15分钟运行一次,但是上传者希望更快地找到答案,因此我正在尝试实现这一目标。 I've created a Windows Service which monitors a folder, and when a file is created, it processes it. 我创建了一个Windows服务,该服务监视一个文件夹,并在创建文件时对其进行处理。
I've followed a couple of different guides, but espically this one http://www.codeproject.com/Articles/32591/Creating-a-Windows-Service-for-Watching-System-Dir 我遵循了一些不同的指南,但是特别是本指南http://www.codeproject.com/Articles/32591/Creating-a-Windows-Service-for-Watching-System-Dir
With the first file it works, but everytime the second file is added, it crashes: 使用第一个文件,它可以工作,但是每次添加第二个文件时,它都会崩溃:
Exception Info: System.IO.IOException
Stack:
at System.IO.__Error.WinIOError(Int32, System.String)
at System.IO.File.InternalCopy(System.String, System.String, Boolean, Boolean)
at System.IO.File.Copy(System.String, System.String)
at FileMonitor.FilKigger.Watcher_Created(System.Object, System.IO.FileSystemEventArgs)
at System.IO.FileSystemWatcher.OnCreated(System.IO.FileSystemEventArgs)
at System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(Int32, System.String)
at System.IO.FileSystemWatcher.CompletionStatusChanged(UInt32, UInt32, System.Threading.NativeOverlapped*)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)
How come is this? 怎么会这样
Some code: 一些代码:
protected override void OnStart(string[] args)
{
FileSystemWatcher Watcher = new FileSystemWatcher();
Watcher.Path = "D:\\FTP\\Batch\\";
Watcher.IncludeSubdirectories = true;
Watcher.Created += new FileSystemEventHandler(Watcher_Created);
Watcher.Deleted += new FileSystemEventHandler(Watcher_Deleted);
Watcher.EnableRaisingEvents = true;
}
And here just the code which copies the file to a new folder. 这里只是将文件复制到新文件夹的代码。
void Watcher_Created(object sender, FileSystemEventArgs e)
{
File.Copy(e.FullPath, "D:\\newFolder\\" + e.Name);
Lg.WriteEntry("File moved", EventLogEntryType.Information);
}
Catching the exception just leaves the file, but ofcourse keeps the service running. 捕获异常只会留下文件,但是当然可以保持服务运行。
Changed the watcher to find "_finish.txt"-files, which the user sends after the real file is uploaded 更改了观察者以查找“ _finish.txt”文件,该文件在实际文件上传后用户发送
Watcher = new FileSystemWatcher(ftpFolder, "*_finish.txt");
Then replacing the _finish with "", and I've got the real file which is guaranteed to be finished uploading... 然后将_finish替换为“”,我已经保证可以完成上传的真实文件...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.