System.IO.File.InternalDelete - 进程无法访问文件

问题描述

我收到错误

System.IO.IOException: 进程无法访问文件,因为它 正在被另一个进程使用。在 System.IO.__Error.WinIOError(Int32 errorCode,String mayFullPath)
在 System.IO.File.InternalDelete(String path,Boolean checkHost)

我正在正确处理所有对象,但不确定为什么会出现该异常。这不会发生在我的本地系统上,但是当我在服务器上部署时,我会间歇性地遇到这个问题。

给定

private byte[] StreamToByteArray(Stream srcFile)
{
    srcFile.Position = 0; //faster than seek when dealing with an absolute 
    position.byte[] buffer = new byte[srcFile.Length];
    srcFile.Read(buffer,buffer.Length);
    return buffer;
}

private string AppendChunkData(ChunkMetaData MetaData,Stream fStream)
{
    string app_Path = Server.MapPath(UploadTemp);
    if (!System.IO.Directory.Exists(app_Path))
        System.IO.Directory.CreateDirectory(app_Path);
    string tempPath = Path.Combine(app_Path,MetaData.UploadUid + MetaData.FileName);
    byte[] byteArray = StreamToByteArray(fStream);
    using (var fileStream = new FileStream(tempPath,FileMode.Append,FileAccess.Write))
    {
        fileStream.Write(byteArray,byteArray.Length);
    }

    return tempPath;
}

使用

string tempPath = AppendChunkData(MetaData,file.InputStream);

//Read file data from temp storage Upload to Object storage 
byte[] content = System.IO.File.ReadAllBytes(tempPath);
  
using (Stream stream = new MemoryStream(content))
{
    f.Upload(stream,data.EntityID,fullPath,data.Domain,data.Org,MetaData.EntityType);
}

if (System.IO.File.Exists(tempPath))
    System.IO.File.Delete(tempPath);   
     

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)