流被关闭仍然出错为什么? [英] stream is closed get still error why?

查看:71
本文介绍了流被关闭仍然出错为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该进程无法访问文件''C:\inetpub \wwwroot \wss \ VirtualDirectories \ 80 \ Upload \ _mydoc_2013-05-03.doc'',因为它正在被另一个进程使用。





 stream =  new  FileStream (filename,FileMode.Open,FileAccess.Read,
FileShare.Read);
// 要读取的总字节数:
long bytesToRead = stream.Length;
Response.ContentType = application / msword;
Response.AddHeader( Content-Disposition attachment; filename = + strFileName);
// 从流中读取小部分的字节数。
while (bytesToRead > 0
{
// 确保客户端仍然连接。
if (Response.IsClientConnected)
{
// 将数据读入缓冲区并写入
// 输出流。
byte [] buffer = new 字节 [ 10000 ];
int length = stream.Read(buffer, 0 10000 );
Response.OutputStream.Write(buffer, 0 ,length);
Response.Flush();
// 我们已经阅读了一些字节..需要阅读
< span class =code-comment> //
仅剩余。
bytesToRead = bytesToRead - length;
}
其他
{
// < span class =code-comment>如果用户不再连接,退出循环..
bytesToRead = -1;
}
}
}
catch (例外情况)
{
Response.Write (ex.Message);
// 发生错误..
}
finally
{
if (stream!= null
{
stream.Close();
stream.Dispose();
}
}
}


}

解决方案

请看我过去的回答:进程无法访问文件''C:\inetpub \ -root \wss \ VirtualDirectories \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ com / Questions / 585393 / Theplusprocesspluscannotplusaccessplustheplusfileptarget =_ blanktitle =New Window> ^ ]。



-SA

The process cannot access the file ''C:\inetpub\wwwroot\wss\VirtualDirectories\80\Upload\mydoc_2013-05-03.doc'' because it is being used by another process.


stream = new FileStream(filename, FileMode.Open, FileAccess.Read,
                                               FileShare.Read);
                       // Total bytes to read:
                       long bytesToRead = stream.Length;
                       Response.ContentType = "application/msword";
                       Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName);
                       // Read the bytes from the stream in small portions.
                       while (bytesToRead > 0)
                       {
                           // Make sure the client is still connected.
                           if (Response.IsClientConnected)
                           {
                               // Read the data into the buffer and write into the
                               // output stream.
                               byte[] buffer = new Byte[10000];
                               int length = stream.Read(buffer, 0, 10000);
                               Response.OutputStream.Write(buffer, 0, length);
                               Response.Flush();
                               // We have already read some bytes.. need to read
                               // only the remaining.
                               bytesToRead = bytesToRead - length;
                           }
                           else
                           {
                               // Get out of the loop, if user is not connected anymore..
                               bytesToRead = -1;
                           }
                       }
                   }
                   catch (Exception ex)
                   {
                       Response.Write(ex.Message);
                       // An error occurred..
                   }
                   finally
                   {
                       if (stream != null)
                       {
                           stream.Close();
                           stream.Dispose();
                       }
                   }
               }


           }

解决方案

Please see my past answer: The process cannot access the file ''C:\inetpub\wwwroot\wss\VirtualDirectories\80\Upload\KingSize_2013-04-29.pdf'' because it is being used by another process.[^].

—SA


这篇关于流被关闭仍然出错为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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