0

我让他遵循代码

    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

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

4

1 回答 1

0

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

尝试做这样的事情:

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

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

于 2012-05-20T07:46:51.477 回答