Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中获取FTP上的文件大小#_C#_Ftp_Size - Fatal编程技术网

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

C# 在C中获取FTP上的文件大小#,c#,ftp,size,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.GetFileS

我想获得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服务器不支持大小方法。

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

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

var size=GetFtpFileSize(新Uri(“ftpURL”)、新网络凭证(“用户名”、“密码”)


我试图在下载时获得文件大小。 这两种情况下,给定的答案对我都不起作用,所以我使用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。

比前面更好的评论:他需要从响应中读取数据,而不仅仅是获取
ContentLength
,我相信。不管怎样,contentlength为0似乎很奇怪。这是一个复制粘贴错误-我更新了我的问题,提供了更多详细信息。我可以在不下载文件的情况下获得文件大小,对吗?我只是不想下载这个文件,因为它很大,如果本地大小相同的话。我想这可能是因为FTP服务器上没有根据谷歌搜索实现大小。我要检查一下。@Andrew Barber-我认为他不想从响应中读取数据,他只想要ContentLength(即文件的长度)
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(" "));