The problem is with FtpDataStream.Read(Byte[] buffer, Int32 offset, Int32 size) method. FtpDataStream closes when it is unable to read and gets disposed. You need to create your own custom FtpDataStream and override the Read method and catch ObjectDisposedException error (return 0) to get you out.The uncaught ObjectDisposedException is bubbling up.
Streamreader has a method called ReadBuffer which calls the FtpDataStream.Read(Byte[] buffer, Int32 offset, Int32 size) method. Since FtpDataStream closes and gets disposed , ReadBuffer never gets to be zero which it needs to be for Streamreader.readline to return null
Declare a custom stream class
public class custStream : Stream
{
internal Stream _stream;
public custStream(Stream stream)
{
_stream = stream;
}
public override void WriteByte(byte value)
{
_stream.WriteByte(value);
}
public override int ReadByte()
{
try
{
return _stream.ReadByte();
}
catch (ObjectDisposedException ode)
{
return 0;
}
}
public override long Seek(long offset, SeekOrigin origin)
{
return _stream.Seek(offset, origin);
}
public override void Flush()
{
_stream.Flush();
}
public override long Position
{
get
{
return _stream.Position;
}
set
{
_stream.Position = value;
}
}
public override void Write(byte[] buffer, int offset, int count)
{
_stream.Write(buffer, offset, count);
}
public override int Read(byte[] buffer, int offset, int count)
{
try
{
return _stream.Read(buffer, offset, count);
}
catch (ObjectDisposedException ode)
{
return 0;
}
}
public override void SetLength(long value)
{
_stream.SetLength(value);
}
public override long Length { get { return _stream.Length; } }
public override bool CanWrite { get { return _stream.CanWrite; } }
public override bool CanSeek { get { return _stream.CanSeek; } }
public override bool CanRead { get { return _stream.CanRead; } }
}
In your code change ftpWebResponse.GetResponseStream() to
using (var responseStream = custStream(ftpWebResponse.GetResponseStream()))