【问题标题】:System.IOException when opening file with File.OpenRead使用 File.OpenRead 打开文件时出现 System.IOException
【发布时间】:2010-08-26 17:45:35
【问题描述】:

当我打开一个文件以解压缩其内容时出现以下异常。当我在 Windows 资源管理器中选择文件或将鼠标悬停在显示工具提示的文件上时会发生这种情况。

System.IO.IOException was unhandled
  Message=The process cannot access the file 'D:\Documents\AutoUnZip\Zips\MVCContrib.Extras.release.zip' because it is being used by another process.
  Source=mscorlib
  StackTrace:
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
       at System.IO.File.OpenRead(String path)
       at AutoUnzip.SelectFolderForm.w_Changed(Object sender, FileSystemEventArgs e) in D:\Projects\WindowsForms\AutoUnzip\AutoUnzip\SelectFolderForm.cs:line 37
       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: 

有没有办法等到文件不再使用然后再读取它?基本上我只是查看一个文件夹中是否有任何新的 zip 文件,解压缩 zip 文件的内容,然后将其删除。

FileSystemWatcher watcher = new FileSystemWatcher("C:\\Path\\To\\Folder\\");
watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName;
watcher.Filter = "*.zip";
watcher.Created += new FileSystemEventHandler(w_Changed);
// Begin watching.
watcher.EnableRaisingEvents = true;

事件处理程序:

void w_Changed(object sender, FileSystemEventArgs e)
{
    // IOException on following line
    using (ZipInputStream s = new ZipInputStream(File.OpenRead(e.FullPath)))
    {
        ...
    }
    // delete the zip file
    File.Delete(e.FullPath);
}

【问题讨论】:

  • 任何带有完整源代码示例的最终解决方案?

标签: c# ioexception filesystemwatcher sharpziplib zipinputstream


【解决方案1】:

当您使用 FileSystemWatcher 时,这是完全正常的。您收到通知的文件很可能正在由创建或修改文件的进程使用。您将不得不等到该过程停止使用它。你当然无法预测何时会发生这种情况。

一种通用的方法是将文件的路径放在您定期扫描的列表中,由计时器触发。最终您将可以访问该文件。

【讨论】:

  • 当另一个进程正在读取文件时,您不能打开文件(用于读取)吗?
  • 通常可以,取决于应用程序。但这不会生成 FSW 通知,因为文件没有被更改。
【解决方案2】:

有时,如果您只是复制错误抛出而不是使用 File.OpenRead,请将其更改为:

void w_Changed(object sender, FileSystemEventArgs e) 
{ 
    // IOException on following line 
    using (ZipInputStream s = new ZipInputStream(new System.IO.FileStream(e.FullPath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite))) 
    { 
        ... 
    } 
    // delete the zip file 
    File.Delete(e.FullPath); 
} 

【讨论】:

    【解决方案3】:

    也许this 有帮助。介绍了几种检查文件是否正在使用的方法...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-22
      • 1970-01-01
      • 2021-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-15
      相关资源
      最近更新 更多