在C#中获取FTP上文件的大小

11

我想获取FTP上文件的大小。

        //Get File Size
        reqSize = (FtpWebRequest)FtpWebRequest.Create(new Uri(FtpPath + filePath));
        reqSize.Credentials = new NetworkCredential(Username, Password);
        reqSize.Method = WebRequestMethods.Ftp.GetFileSize;
        reqSize.UseBinary = true;
        FtpWebResponse respSize = (FtpWebResponse)reqSize.GetResponse();
        long size = respSize.ContentLength;
        respSize.Close();

我尝试过以下方法,但是得到了550错误。文件未找到/无访问权限。 然而,以下代码可以正常工作...

                reqTime = (FtpWebRequest)FtpWebRequest.Create(new Uri(FtpPath + filePath));
                reqTime.Credentials = new NetworkCredential(Username, Password);
                reqTime.Method = WebRequestMethods.Ftp.GetDateTimestamp;
                reqTime.UseBinary = true;
                FtpWebResponse respTime = (FtpWebResponse)reqTime.GetResponse();
                DateTime LastModified = respTime.LastModified;
                respTime.Close();

编辑:这不起作用的原因是我的FTP服务器不支持SIZE方法。

3个回答

30

尝试使用 reqSize.Method = WebRequestMethods.Ftp.GetFileSize; 而不是 GetDateTimestamp

这对我起作用了:

FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://servername/filepath"));
request.Proxy = null;
request.Credentials = new NetworkCredential("user", "password");
request.Method = WebRequestMethods.Ftp.GetFileSize;

FtpWebResponse response = (FtpWebResponse)request.GetResponse();
long size = response.ContentLength;
response.Close();

比之前好的评论:我相信他需要从响应中读取数据,而不仅仅是获取“ContentLength”。无论如何,内容长度为0似乎很奇怪。 - Andrew Barber
那是一个复制粘贴错误 - 我已经更新了我的问题并提供了更多的细节。 - Jason
我可以在不下载文件的情况下获取文件大小,对吧?如果本地文件大小相同,我只是不想下载这个巨大的文件。 - Jason
我认为可能是因为一些 Google 搜索结果表明 FTP 服务器上未实现 SIZE 功能。我将对此进行核实。 - Jason
应省略第二个和第三个GetResponse()。如果我调用三次GetResponse(),所有响应都相等(在立即窗口中比较==)。结果在loginresponse.ContentLength中。 - huha
显示剩余3条评论

2

我尝试在下载文件时获取文件大小。然而,之前尝试的方法都没有奏效,于是我使用FtpWebResponse的StatusDescription属性和DownloadFile方法来实现:

int openingBracket = response.StatusDescription.IndexOf("(");
int closingBracket = response.StatusDescription.IndexOf(")");                     
var temporarySubstring = response.StatusDescription.Substring(openingBracket+1, closingBracket - openingBracket);
var fileSize = temporarySubstring.Substring(0, temporarySubstring.IndexOf(" "));

我想使用正则表达式有更好的方法来完成这个任务。


1

//获取FTP文件大小的最简单和高效的方法。

var size = GetFtpFileSize(new Uri("ftpURL"), new NetworkCredential("userName", "password"));

public static long GetFtpFileSize(Uri requestUri, NetworkCredential networkCredential)
{
    //Create ftpWebRequest object with given options to get the File Size. 
    var ftpWebRequest = GetFtpWebRequest(requestUri, networkCredential, WebRequestMethods.Ftp.GetFileSize);

    try { return ((FtpWebResponse)ftpWebRequest.GetResponse()).ContentLength; } //Incase of success it'll return the File Size.
    catch (Exception) { return default(long); } //Incase of fail it'll return default value to check it later.
}
public static FtpWebRequest GetFtpWebRequest(Uri requestUri, NetworkCredential networkCredential, string method = null)
{
    var ftpWebRequest = (FtpWebRequest)WebRequest.Create(requestUri); //Create FtpWebRequest with given Request Uri.
    ftpWebRequest.Credentials = networkCredential; //Set the Credentials of current FtpWebRequest.

    if (!string.IsNullOrEmpty(method))
        ftpWebRequest.Method = method; //Set the Method of FtpWebRequest incase it has a value.
    return ftpWebRequest; //Return the configured FtpWebRequest.
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接