0

当我想在我的 ftp 服务器上获取文件的大小时,我什么也得不到。

有人在代码中看到问题吗?

在我看来这个方法很好

班上 :

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); }));
}

感谢帮助。

4

1 回答 1

3

您需要使用 ContentLength 属性来获取文件大小

Console.WriteLine(ftpResponse.ContentLength.ToString());

某些 ftp 服务器不支持getfilesize,因此您必须使用ListDirectoryDetails

于 2013-02-22T14:50:43.757 回答