使用C#在FTP上获取文件大小

时间:2010-11-14 02:24:00

标签: c# ftp size

我想在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 个答案:

答案 0 :(得分:25)

试试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();

答案 1 :(得分:1)

我试图在下载文件时获取文件大小。 给出的答案都对我不起作用,因此我通过使用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(" "));

我想用RegEx可以有更好的方法。

答案 2 :(得分:0)

//获取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.
}
相关问题