使用File.OpenRead打开文件时出现System.IOException [英] System.IOException when opening file with File.OpenRead

查看:451
本文介绍了使用File.OpenRead打开文件时出现System.IOException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I get the following exception when I open a file for unzipping it's contents. It happens when I have the file selected in Windows Explorer, or mouse over it showing a tooltip.

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文件的内容,然后将其删除.

Is there a way just to wait until the file is no longer in use and then read it? Basically I just watch a folder for any new zip files, unzip the contents of the zip file, then delete it.

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);
}

推荐答案

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

This is entirely normal when you use FileSystemWatcher. It is very likely that the file you get the notification for is in use by the process that created or modified the file. You will have to wait until the process stops using it. You of course cannot predict when that happens.

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

A generic approach is to put the path to the file in a list that you periodically scan, triggered by a timer. Eventually you'll get access to the file.

这篇关于使用File.OpenRead打开文件时出现System.IOException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆