如何检测FTP下载的文件是否完成?
你好,
我正在使用一个简单的程序识别用户指定的某个文件夹中文件的变化。
当我通过FTP下载文件时,会创建2个文件(一个具有最终名称(例如:.iso)和其他.part),并触发两个新文件事件。有什么方法可以知道下载的文件何时完成?
问候,
Pedro
Hello,
I'm using a simple program that identifies changes in files in a certain folder specified by the user.
When I download a file through FTP, 2 files are created (one with final name (ex: .iso) and other .part) and two new file events are triggered. Is there any method to know when the downloaded file is completed?
Regards,
Pedro
查看代码的最后一行,即 Console.WriteLine()。
See the very last line of the code i.e Console.WriteLine().
using System;
using System.IO;
using System.Net;
using System.Text;
namespace Examples.System.Net
{
public class WebRequestGetExample
{
public static void Main ()
{
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
request.Method = WebRequestMethods.Ftp.DownloadFile;
// This example assumes the FTP site uses anonymous logon.
request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
Console.WriteLine(reader.ReadToEnd());
Console.WriteLine("Download Complete, status {0}", response.StatusDescription);
reader.Close();
response.Close();
}
}
}
From MSDN[^]
-KR
如果你正在谈论你正在使用的外部FTP应用程序,与你自己的代码分开,没有办法确定文件下载何时完成。
但是,您可以尝试打开该文件,拒绝对其他应用程序的共享访问。当您最终可以打开文件时,FTP下载完成。在此之前,下载仍在继续。
If you're talking about an external FTP app that you're using, seperate from your own code, no there is no way to determine when that file download is complete.
You can, however, just try to open the file, denying shared access to other applications. When you can finally open the file, the FTP download is done. Until then, the download is still going on.
您可以通过检查ftp文件大小的下载文件大小来验证已完全下载的文件
获取ftp文件大小使用以下功能
you can verify ur file completely downloaded by checking downloaded file size with ftp file size
for getting ftp file size use below function
public static long FileSize(string FTPFullFileName, string UserName, string Password)
{
try
{
FtpWebRequest FTP = (FtpWebRequest)FtpWebRequest.Create(FTPFullFileName);
FTP.Method = WebRequestMethods.Ftp.GetFileSize;
FTP.UseBinary = true;
FTP.Credentials = new NetworkCredential(UserName, Password);
FtpWebResponse Response = (FtpWebResponse)FTP.GetResponse();
Stream FtpStream = Response.GetResponseStream();
long FileSize = Response.ContentLength;
FtpStream.Close();
Response.Close();
return FileSize;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
调用以上功能
calling above function
string ftpFileSize =Convert.ToString(FileSize(FTPFullFileName, UserName, Password)) ;
使用fileinfo获取下载的文件大小
fetching downloaded file size using fileinfo
string localFileSize=string.Empty;
FileInfo info = new FileInfo("downloaded file path+filename");
if (info.Exists)
{
localFileSize = info.Length.ToString();
}
如果localFileSize等于ftpFileSize,你下载完成
u必须确保localFileSize& ftpFileSize是相同的单位,否则将它们转换为相同的单位
gud运气; - )
if localFileSize equals ftpFileSize ,ur download is complete
u have to make sure localFileSize & ftpFileSize are in same units else convert them to same unit
gud luck ;-)