I was trying to copy files from FTP using FTPWebRequest in my wcf block here is the code below.
long cl = response.ContentLength;
int bufferSize = 4155;
int readCount=0;
int i = 0;
byte[] newbuffer;
byte[] buffer = new byte[bufferSize];
//Follow the Using here
using (Stream input = response.GetResponseStream())
{
readCount = input.Read(buffer, 0, buffer.Length);
if (readCount > 0)
{
newbuffer = new byte[readCount];
Array.Copy(buffer, newbuffer, readCount);
outputStream.Write(newbuffer.ToArray(), 0, readCount);
}
outputStream.Close();
}
ftpStream.Close();
response.Close();
}
in the “USING” block was suggested by “DotNetUser” member,i found one thing in the code
outputStream.Write(newbuffer.ToArray(), 0, readCount); throwing an exception at “ToArray”,it suppose to be like outputStream.Write(newbuffer, 0, readCount);please suggest me.
The
newbuffervariable is already an array, so you don’t need to convert it to an array. You can just use:However, there is no reason to create the
newbufferarray at all. Just write the data from thebufferarray:However, this will only read the first block of data from the stream. You need to loop until there is no more data to read: