为什么我收到异常:ObjectDisposedException?(Why i'm getting exception: ObjectDisposedException?)

其他开发 IT屋
问 题

This is the code:

FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(responseStream, System.Text.Encoding.UTF8);
long filel = readStream.ReadToEnd().Length;
readStream.Close();
FileStream writeStream = new FileStream(ftpdirectories + "\\" + filenameonly, FileMode.Create);
string fnn = ftpdirectories + "\\" + filenameonly;
int Length = 2048;
Byte[] buffer = new Byte[Length];
int bytesRead = responseStream.Read(buffer, 0, Length);

The exception is on the line:

int bytesRead = responseStream.Read(buffer, 0, Length);

If i'm not using the StreamReader the long and the Close it's working fine but once i'm adding the StreamReader i'm getting the exception.

Cannot access a disposed object.

Object name: 'System.Net.Sockets.NetworkStream'.

 System.ObjectDisposedException was caught
      HResult=-2146232798
      Message=Cannot access a disposed object.
    Object name: 'System.Net.Sockets.NetworkStream'.
      Source=System
      ObjectName=System.Net.Sockets.NetworkStream
      StackTrace:
           at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
           at System.Net.FtpDataStream.Read(Byte[] buffer, Int32 offset, Int32 size)
           at FTP_ProgressBar.FtpProgress.DownloadFtpContent(Object sender, String file, String filesdirectories, String fn) in c:\ftp_progressbar\FTP_ProgressBar\FtpProgress.cs:line 284
      InnerException:

Line 284 is:

int bytesRead = responseStream.Read(buffer, 0, Length);
解决方案

ObjectDisposedException: The exception that is thrown when an operation is performed on a disposed object.

when you close readStream

readStream.Close();

StreamReader.Close Method Closes the StreamReader object and the underlying stream, and releases any system resources associated with the reader. This implementation of Close calls the Dispose method passing a true value.

the underlying responseStream which is set in

StreamReader readStream = new StreamReader(responseStream, ...

is closed and closing a stream call dispose method and it get disposed. after that you access responseStream and boom! ObjectDisposedException

本文地址:IT屋 » Why i'm getting exception: ObjectDisposedException?

问 题

这是代码:

  FtpWebResponse response =(FtpWebResponse)reqFTP.GetResponse(); 
Stream responseStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(responseStream,System.Text.Encoding.UTF8);
long filel = readStream.ReadToEnd()。Length;
readStream.Close();
FileStream writeStream = new FileStream(ftpdirectories +“\\”+ filenameonly,FileMode.Create);
string fnn = ftpdirectories +“\\”+ filenameonly;
int长度= 2048;
Byte [] buffer = new Byte [Length];
int bytesRead = responseStream.Read(buffer,0,Length);


例外如下:

  int bytesRead = responseStream.Read(buffer,0,Length); 


如果我没有使用StreamReader,并且关闭它工作正常,但是一旦我添加StreamReader我收到了异常。



无法访问已处理的对象。



对象名称:' System.Net.Sockets.NetworkStream'。



  System.ObjectDisposedException被捕获
HResult = -2146232798
消息=无法访问处置的对象。
对象名称:'System.Net.Sockets.NetworkStream'。
Source = System
ObjectName = System.Net.Sockets.NetworkStream
System.Net.Sockets.NetworkStream.Read(Byte [] buffer,Int32 offset,Int32大小)
at System.Net.FtpDataStream.Read(Byte [] buffer,Int32 offset,Int32 size)
at FTP_ProgressBar.FtpProgress.DownloadFtpContent(Object sender,String file,String filesdirectories,String fn)in c:\ftp_progressbar\FTP_ProgressBar\FtpProgress.cs:第284行
InnerException:


第284行是:

  int bytesRead = responseStream.Read(buffer,0,Length); 

解决方案

ObjectDisposedException :操作时引发的异常是在处理过的对象上执行的。




当您关闭 readStream



  readStream.Close(); 



StreamReader.Close方法关闭StreamReader对象和基础流,并发布任何与读者相关的系统资源。 Close的这个实现调用Dispose方法传递一个真实值。




底层 responseStream

  StreamReader readStream = new StreamReader(responseStream,... 


关闭并关闭一个流调用的dispose方法,然后它被抛弃,之后你访问 responseStream 和boom! ObjectDisposedException


本文地址:IT屋 » 为什么我收到异常:ObjectDisposedException?