我想获取 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 上的文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!