我想要获取每个文件在FTP服务器上的文件名,文件大小和上次修改时间,然后用它填充listView。C#使用ftpRequest获取完整文件列表的速度很慢
它工作得很好,直到我切换FTP主机,现在它真的很慢,尽管新的主机在真正的FTP客户端速度一样快。
明显的原因是为什么?
另外,每次发送登录凭证真的需要吗?
我使用第一种方法来获得一个字符串数组,然后遍历它并使用在各个项目上第二个获得文件大小:
public static string[] GetFileList()
{
string[] downloadFiles;
StringBuilder result = new StringBuilder();
FtpWebRequest request;
try
{
request = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://mysite.se/"));
request.UseBinary = true;
request.Credentials = new NetworkCredential(settings.Username, settings.Password);
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.UseBinary = true;
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("\n");
line = reader.ReadLine();
}
// to remove the trailing '\n'
result.Remove(result.ToString().LastIndexOf('\n'), 1);
reader.Close();
response.Close();
return result.ToString().Split('\n');
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
downloadFiles = null;
return downloadFiles;
}
}
public static int GetFileSize(string file)
{
//MessageBox.Show("getting filesize...");
StringBuilder result = new StringBuilder();
FtpWebRequest request;
try
{
request = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://mysite.se/" + file));
request.UseBinary = true;
request.Credentials = new NetworkCredential(settings.Username, settings.Password);
request.Method = WebRequestMethods.Ftp.GetFileSize;
int dataLength = (int)request.GetResponse().ContentLength;
return dataLength;
}
catch (Exception ex)
{
//System.Windows.Forms.MessageBox.Show(ex.Message);
return 1337;
}
}