2013-02-22 72 views
0

当我想要在我的ftp服务器上获取文件大小时,我什么也得不到。C#在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); })); 
} 

感谢您的帮助。

+0

你确定目标服务器的FTP实现支持那些信息?不要以为他们都这样做。 – 2013-02-22 14:41:44

+0

请参阅此问题的答案:http://stackoverflow.com/questions/4175874/get-file-size-on-an-ftp-in-c-sharp – Pete 2013-02-22 14:43:03

+0

你解决了吗? – 2013-05-17 07:38:14

回答

2

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

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

有些FTP服务器不支持getfilesize,所以你将不得不使用ListDirectoryDetails