流或文件未关闭

本文关键字:文件 | 更新日期: 2023-09-27 18:34:47

我有他下面的代码

    HttpPostedFileBase files = Request.Files[0];
    Stream fileStream = files.InputStream;
    using (Stream file = System.IO.File.OpenWrite(originalImage))
    {
        StreamUtil.CopyStream(fileStream, file);
        file.Close();
        fileStream.close();
        fileStream.dispose();
    }
    // here is CopyStream method
    public static void CopyStream(Stream input, Stream output)
    {
        var buffer = new byte[8*1024];
        int len;
        while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            output.Write(buffer, 0, len);
        }
    }

当我尝试两次写入同一个文件时,我得到

The process cannot access the file 'u0027D:FILENAME because it is being used by another process

我怎样才能关闭它,所以一旦请求完成写入,它就会被关闭?

流或文件未关闭

我想

把它写成评论,但不清楚:

尝试执行以下操作:

MemoryStream fileStream = new MemoryStream();
fileStream.WriteByte(04);
using (Stream file = System.IO.File.OpenWrite(originalImage))
{
    CopyStream(fileStream, file);
}
using (Stream file = System.IO.File.OpenWrite(originalImage))
{
    CopyStream(fileStream, file);
}

您是否在第二个使用行上遇到相同的错误?