使用System.IO.FileSystemWatcher功能处理IOException异常

问题描述:

基本上我想要做的是观察新文件,该文件被放入具有特定文件扩展名(* .req)的文件夹中,因此我开始使用系统进行设置.IO.FileSystemWatcher功能。所以在我开始我的程序之后,我只需将一组* .req文件复制到我的监视文件夹中,并且他们似乎在第一次运行时没有任何错误。随后将文件重命名,处理,然后删除。我随后将同* .req文件重新拷贝过来,现在这个时候它吹一个IOException是未处理的错误在细节如下:使用System.IO.FileSystemWatcher功能处理IOException异常

System.IO.IOException was unhandled 
    HResult=-2147024864 
    Message=The process cannot access the file 'C:\accucom\reqdir\hcd - Copy (2).req' because it is being used by another process. 
    Source=mscorlib 
    StackTrace: 
    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 
    at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost) 
    at System.IO.File.Copy(String sourceFileName, String destFileName) 
    at CollectV2.CollectV2.OnChanged(Object source, FileSystemEventArgs e) in C:\accucom\CollectV2\CollectV2.cs:line 375 
    at System.IO.FileSystemWatcher.OnCreated(FileSystemEventArgs e) 
    at System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(Int32 action, String name) 
    at System.IO.FileSystemWatcher.CompletionStatusChanged(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* overlappedPointer) 
    at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP) 
    InnerException: 

下面是我使用的代码片段,因为我不知道我可能会丢失在这里:

 FileSystemWatcher watcher = new FileSystemWatcher(); 
     watcher.Path = GlobalVars.REQpath; 
     // Only watch *.req files. 
     watcher.Filter = "*.req"; 
     watcher.Created += new FileSystemEventHandler(OnChanged); 

     // Begin watching. 
     watcher.EnableRaisingEvents = true; 

     // Wait for the user to quit the program. 
     Console.WriteLine("Press \'q\' to quit the sample."); 
     while (Console.Read() != 'q') ; 

    // Define the event handlers. 
    private static void OnChanged(object source, FileSystemEventArgs e) 
    { 
     // Specify what is done when a file is changed, created, or deleted. 
     Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType); 
     string rqfile = e.FullPath.Replace(".req", ".re0"); 
     //System.IO.File.Delete(rqfile); 
     System.IO.File.Copy(e.FullPath, rqfile); 
     System.IO.File.Delete(e.FullPath); 

     ThreadPool.QueueUserWorkItem(wcbdoRequestFile, rqfile); 
    } 

我认为这个问题是在这些线路:

System.IO.File.Copy(e.FullPath, rqfile); 
System.IO.File.Delete(e.FullPath); 

我猜测,问题是,你的文件作出回应,而过程改变它正在改变ST生病了,它打开了。

理想的解决方案是更改文件的程序,以便在程序结束并关闭文件时通知程序。

如果失败了,我发现的唯一方法就是反复尝试复制/删除操作,直到成功为止,并在每次尝试之间等待一会儿。这当然不是很好,但是如果不能与其他程序进行交流,那可能就是你所能做的。

+0

原来的办法我只好这是保持阅读任何* .req文件的文件夹目录使用以下命令之间的睡眠(250): files = System.IO.Directory.GetFiles(GlobalVars.REQpath,“* .req”,SearchOption.TopDirectoryOnly); 我遇到的问题是路径位于客户端程序可以放置的网络文件夹中,但我注意到在专用服务器实际使用此方法看到文件之前可能需要5-6秒,所以我如果可能的话寻找更快的方法,并且必须最终没有错误地工作。 – jfalberg

最后我用它回避我得到错误,但仍然不明白为什么它不得不等待一段过程来完成下面的代码:

 System.IO.File.Delete(rqfile); 
     while (true) { 
      try 
      { 
       System.IO.File.Copy(e.FullPath, rqfile); 
       break; 
      } 
      catch { } 
     } 
     System.IO.File.Delete(e.FullPath);