Hi,
I am using this code to download files on VS 2005. It downloads the file but
waits until the timeout (which is why it is so short) to add the last 4
bytes or so. It always returns a WebException which most of the time is just
feedback from the FTP server, so how do I tell if the download was
successful/complete or not?
Tim
public void DownloadFile(string fileName, string mfgrId)
{
try
{
string destinationFile = @"C:\Program Files\MM\" + fileName;
FileInfo file = new FileInfo(destinationFile);
FileStream localfileStream;
FtpWebRequest request = WebRequest.Create("ftp://" + hostname + downloadDir
+ fileName) as FtpWebRequest;
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Timeout = 5000; //five seconds
if (file.Exists)
{
request.ContentOffset = file.Length;
localfileStream = new FileStream(destinationFile, FileMode.Append,
FileAccess.Write);
}
else
{
localfileStream = new FileStream(destinationFile, FileMode.Create,
FileAccess.Write);
}
WebResponse response = request.GetResponse();
Stream responseStream = response.GetResponseStream();
byte[] buffer = new byte[1024];
int bytesRead = responseStream.Read(buffer, 0, 1024);
while (bytesRead != 0)
{
localfileStream.Write(buffer, 0, bytesRead);
bytesRead = responseStream.Read(buffer, 0, 1024);
}
localfileStream.Close();
responseStream.Close();
ErrorReporter.LogableError(fileName + " downloaded successfully",
ErrorReporter.ErrorTypes.DDS);
}
finally
{
}
}