在 C# 中获取 FTP 上的文件大小 [英] Get File Size On An FTP in C#

查看:107
本文介绍了在 C# 中获取 FTP 上的文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我尝试了以下方法,但收到 550 错误.找不到文件/无法访问.但是,以下代码有效...

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

这对我不起作用的原因是我的 FTP 服务器不支持 SIZE 方法.

推荐答案

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

Try reqSize.Method = WebRequestMethods.Ftp.GetFileSize; instead of 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();

这篇关于在 C# 中获取 FTP 上的文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆