我想在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();
Run Code Online (Sandbox Code Playgroud)
我尝试了以下但得到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();
Run Code Online (Sandbox Code Playgroud)
编辑:这对我不起作用的原因是我的FTP服务器不支持SIZE方法.
bbo*_*sak 26
尝试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();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
34452 次 |
| 最近记录: |