I have a Windows-service and this service will connect to an ftp server and do download, upload, and rename operations.
My service runs every 30 seconds, so the ftp server will be busy during these operations.
My question is: is my code useful for this job? Because my download/upload/rename functions open a webrequest each process? I use FtpWebRequest
and FtpWebResponse
I write only the download functions, the other functions are similar.
Download function:
public Result Download(string localPath,string remotePath,string fileName)
{
Result _result = new Result();
FtpWebRequest requestFileDownload = null;
FtpWebResponse responseFileDownload = null;
try
{
//
requestFileDownload =
(FtpWebRequest)WebRequest.Create(remotePath + fileName);
//
requestFileDownload.Credentials =
new NetworkCredential(this.Username, this.Password);
//
requestFileDownload.Method =
WebRequestMethods.Ftp.DownloadFile;
responseFileDownload =
(FtpWebResponse)requestFileDownload.GetResponse();
using (Stream responseStream = responseFileDownload.GetResponseStream())
{
FileStream writeStream = new FileStream(localPath + fileName, FileMode.Create);
int Length = 2048;
Byte[] buffer = new Byte[Length];
int bytesRead;
while((bytesRead = responseStream.Read(buffer, 0, Length)) > 0)
{
writeStream.Write(buffer, 0, bytesRead);
}
writeStream.Close();
}
return _result;
}
catch (Exception x)
{
_result.Error = true;
_result.ErrorMessage = x.StackTrace;
_result.ErrorFileName = fileName;
_result.ErrorFilePath = remotePath + fileName;
return _result;
}
finally
{
requestFileDownload = null;
responseFileDownload = null;
}
}
public struct Result
{
public bool Error;
public string ErrorMessage;
public string ErrorFileName;
public string ErrorFilePath;
public List<string> Files;
}