System.IOException when opening file with File.OpenRead

走远了吗. 提交于 2019-12-11 17:26:42

问题


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: 

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;

Event handler:

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

回答1:


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.




回答2:


Maybe this helps. Describes a few ways to check if a file is being used...




回答3:


Sometimes if you are just copying the error throw anyway instead of using File.OpenRead change it to:

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


来源:https://stackoverflow.com/questions/3577889/system-ioexception-when-opening-file-with-file-openread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!