本文介绍了获得文件大小在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服务器不支持该尺寸的方法。
推荐答案尝试 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
发布评论