在我的应用程序中,我需要从 FTP 服务器下载一些文件。我通过 for 循环进行编码以逐个下载,它工作正常。我的问题是它的每个文件的处理速度都很慢,因为它会提供凭据。因此,如果我可以将这些文件压缩到 FTP 服务器中,则可以快速下载。如果有办法请指导我。这里我的代码:
块引用
private void button1_Click(object sender, EventArgs e)
{
string[] list = GetFileList();
DownloadFile(list, "e:\\sample\\");
}
private String[] GetFileList()
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(_remoteHost));
request.Credentials = new NetworkCredential(_remoteUser, _remotePass);
request.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string FileNames = reader.ReadToEnd();
string[] Files = Regex.Split(FileNames, "\r\n");
return Files;
}
private void DownloadFile(string[] fileList, string destination)
{
for (int i = 2; i <= fileList.Length - 1; i++)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(_remoteHost + fileList[i]);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(_remoteUser, _remotePass);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
StreamWriter writer = new StreamWriter(destination + fileList[i]);
writer.Write(reader.ReadToEnd());
writer.Close();
reader.Close();
response.Close();
}
}
块引用