which ftp download method is better? or doesn't matter?

  • Thread starter Thread starter Rich P
  • Start date Start date
R

Rich P

I have 2 methods for downloading a file from an ftp server

method1 - has less lines of code than method2 and does not use a while
loop like method2 - but both methods work fine. Is there anything
special in method2 that would make it more robust than method1 ?

using System;
...
using System.Net;

//method1
WebClient request = new WebClient();
request.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
byte[] fileData = request.DownloadData("ftp://" + ftpServerIP + "/" +
fileName);
FileStream file = File.Create(filePath + "\\" + fileName);
file.Write(fileData, 0, fileData.Length);
file.Close();


//method2
FileStream outputStream = new FileStream(filePath + "\\" + fileName,
FileMode.Create);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" +
ftpServerIP + "/" + fileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();


Thanks,


Rich
 
I left out a line in method2 on 1st post


//method1
WebClient request = new WebClient();
request.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
byte[] fileData = request.DownloadData("ftp://" + ftpServerIP + "/" +
fileName);
FileStream file = File.Create(filePath + "\\" + fileName);
file.Write(fileData, 0, fileData.Length);
file.Close();


//method2
FtpWebRequest reqFTP; //<--- I left this out on 1st post

FileStream outputStream = new FileStream(filePath + "\\" + fileName,
FileMode.Create);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" +
ftpServerIP + "/" + fileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();

Rich
 
As always this a tradeoff... #1 is simpler but #2 likely uses less memory
and would allow to add progress reporting...
 
Rich said:
I have 2 methods for downloading a file from an ftp server

method1 - has less lines of code than method2 and does not use a while
loop like method2 - but both methods work fine. Is there anything
special in method2 that would make it more robust than method1 ?

using System;
..
using System.Net;

//method1
WebClient request = new WebClient();
request.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
byte[] fileData = request.DownloadData("ftp://" + ftpServerIP + "/" +
fileName);
FileStream file = File.Create(filePath + "\\" + fileName);
file.Write(fileData, 0, fileData.Length);
file.Close();


//method2
FileStream outputStream = new FileStream(filePath + "\\" + fileName,
FileMode.Create);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" +
ftpServerIP + "/" + fileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();

As described I would consider the first method the best, because
it does the same task with simpler code.

WebRequest is not useless. Sometimes you do want to execute some
special code inside the loop etc., but if you don't need to do
something special, then you do not need that.

Arne
 
Back
Top