我正在从ftp服务器中提取文件,而我在验证传输成功完成的方法时遇到了麻烦。
感觉必须有更具体的方法来检测成功转移。有什么想法吗?
我的代码:
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;
答案 0 :(得分:5)
检查FtpWebResponse.StatusCode
是否成功。例如FtpStatusCode.ClosingData