4

我正在从 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;
4

1 回答 1

5

检查FtpWebResponse.StatusCode成功。例如FtpStatusCode.ClosingData

于 2012-05-18T13:32:39.687 回答