C#不';无法在FTP服务器上获取文件大小
当我想获得ftp服务器上文件的大小时,我什么也得不到 有人看到代码中有问题吗 在我看来,这种方法是好的 班级:C#不';无法在FTP服务器上获取文件大小,c#,ftp,filesize,C#,Ftp,Filesize,当我想获得ftp服务器上文件的大小时,我什么也得不到 有人看到代码中有问题吗 在我看来,这种方法是好的 班级: public string getFileSize(string fileName) { try { ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + fileName); ftpRequest.Credentials = new NetworkCredential
public string getFileSize(string fileName)
{
try
{
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + fileName);
ftpRequest.Credentials = new NetworkCredential(user, pass);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpStream = ftpResponse.GetResponseStream();
StreamReader ftpReader = new StreamReader(ftpStream);
string fileInfo = null;
try { while (ftpReader.Peek() != -1) { fileInfo = ftpReader.ReadToEnd(); } }
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
ftpReader.Close();
ftpStream.Close();
ftpResponse.Close();
ftpRequest = null;
return fileInfo;
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
return "";
}
过程:
ftp ftpClientCheckFile = new ftp(@"ftp://******.******.fr", "***********", "********");
string ftpfileSize = ftpClientCheckFile.getFileSize(fileName);
if (ftpfileSize == localfilesize)
{
this.Invoke(new Action(() => { MessageBox.Show(this, "*********", "***", MessageBoxButtons.OK, MessageBoxIcon.Information); }));
ftpClientCheckFile = null;
}
else
{
this.Invoke(new Action(() => { MessageBox.Show(this, "***** ", "*******", MessageBoxButtons.OK, MessageBoxIcon.Information); }));
}
谢谢您的帮助。您需要使用ContentLength属性来获取文件大小
Console.WriteLine(ftpResponse.ContentLength.ToString());
某些ftp服务器不支持
getfilesize
,因此您必须使用ListDirectoryDetails
您确定目标服务器的ftp实现支持该信息吗?不要认为他们都这样。看看这个问题的答案:你解决了这个问题吗?