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
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