Reputation: 18162
I am pulling a file from an ftp server and I'm having trouble feeling comfortable with my method of verifying the transfer was completed successfully.
It feels like there must be a more concrete way of detecting a successful transfer. Any ideas?
My Code:
var request = (FtpWebRequest)FtpWebRequest.Create(ftpFilePath);
request.KeepAlive = false;
request.UseBinary = true;
request.UsePassive = false;
request.Credentials = new NetworkCredential("Username", "Password");
request.Method = WebRequestMethods.Ftp.DownloadFile;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
using (var stream = response.GetResponseStream())
{
using (var reader = new StreamReader(stream))
{
contents = reader.ReadToEnd();
}
}
//Check to see if transfer was successful
if (response.StatusDescription.StartsWith("2"))
transferSuccessful = true;
Upvotes: 4
Views: 3757
Reputation: 35870
Check the FtpWebResponse.StatusCode
for success. e.g. FtpStatusCode.ClosingData
Upvotes: 5