![](/img/trans.png)
[英].NET How to combine FileSystemWatcher with ReadDirectoryChangesW to monitor Folder/File Opening?
[英]Monitor folder with FileSystemWatcher fails after first time
每天我常常收到來自不同用戶的xml文件。 這些文件通過FTP傳輸到我的驅動器上的文件夾,例如D:\\ batch。 今天,我使用計划任務檢查這些文件,該任務將啟動ASP.NET頁並處理找到的文件並回答上傳器並結束。 該任務每15分鍾運行一次,但是上傳者希望更快地找到答案,因此我正在嘗試實現這一目標。 我創建了一個Windows服務,該服務監視一個文件夾,並在創建文件時對其進行處理。
我遵循了一些不同的指南,但是特別是本指南http://www.codeproject.com/Articles/32591/Creating-a-Windows-Service-for-Watching-System-Dir
使用第一個文件,它可以工作,但是每次添加第二個文件時,它都會崩潰:
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*)
怎么會這樣
一些代碼:
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;
}
這里只是將文件復制到新文件夾的代碼。
void Watcher_Created(object sender, FileSystemEventArgs e)
{
File.Copy(e.FullPath, "D:\\newFolder\\" + e.Name);
Lg.WriteEntry("File moved", EventLogEntryType.Information);
}
捕獲異常只會留下文件,但是當然可以保持服務運行。
更改了觀察者以查找“ _finish.txt”文件,該文件在實際文件上傳后用戶發送
Watcher = new FileSystemWatcher(ftpFolder, "*_finish.txt");
然后將_finish替換為“”,我已經保證可以完成上傳的真實文件...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.