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

发布于 2024-10-02 12:12:19 字数 1076 浏览 4 评论 0原文

我想获取 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 方法。

I want to get the size of a file on an 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();

I have tried the following but get a 550 error. File not found / no access. However, the following code works...

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

EDIT: The reason this isn't working for me is that my FTP Server does not support the SIZE method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青巷忧颜 2024-10-09 12:12:19

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

Try reqSize.Method = WebRequestMethods.Ftp.GetFileSize;
instead of GetDateTimestamp

This worked for me:

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();
毁梦 2024-10-09 12:12:19

我试图在下载时获取文件大小。
给出的答案对我来说都不起作用,所以我使用 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 来做到这一点。

I tried to get file size while downloading it.
Both, given answers doesn't work for me, so I made this, using StatusDescription property of FtpWebResponse with DownloadFile method:

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

I suppose there is better way to do this with RegEx.

太傻旳人生 2024-10-09 12:12:19

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

var size = GetFtpFileSize(new Uri("ftpURL"), new NetworkCredential("用户名", "密码"));

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.
}

//Simplest and Efficient way to Get FTP File Size.

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.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文