在 C# 中获取 FTP 上的文件大小
我想获取 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试 reqSize.Method = WebRequestMethods.Ftp.GetFileSize;
而不是 GetDateTimestamp
这对我有用:
Try
reqSize.Method = WebRequestMethods.Ftp.GetFileSize;
instead of GetDateTimestamp
This worked for me:
我试图在下载时获取文件大小。
给出的答案对我来说都不起作用,所以我使用 FtpWebResponse 的 StatusDescription 属性和 DownloadFile 方法做了这个:
我想有更好的方法可以使用 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:
I suppose there is better way to do this with RegEx.
//获取 FTP 文件大小的最简单有效的方法。
var size = GetFtpFileSize(new Uri("ftpURL"), new NetworkCredential("用户名", "密码"));
//Simplest and Efficient way to Get FTP File Size.
var size = GetFtpFileSize(new Uri("ftpURL"), new NetworkCredential("userName", "password"));