【发布时间】:2018-09-09 14:02:03
【问题描述】:
我有一个工作代码可以从 FTP 服务器下载许多文件(数百个),但它非常慢并且经常出现超时错误。
这是我目前的下载方式:
Using ftpClient As New WebClient()
ftpClient.Credentials = New System.Net.NetworkCredential(ftpuser, ftppassword)
For i As Integer = 0 To directoriesDownload.Count - 1
If directoriesDownload(i).Contains(".") Then
If Sync_BackgroundWorker.CancellationPending = True Then
Exit Sub
End If
Dim path As String = "ftp://" & ftpserver & "Datenbank/" + directoriesDownload(i).ToString()
Dim trnsfrpth As String = config.rootpath & "ServerDownload\" + directoriesDownload(i).ToString()
ftpClient.DownloadFile(path, trnsfrpth)
filenametodownload = directoriesDownload(i).ToString()
filesdownloaded += 1
Sync_BackgroundWorker.ReportProgress(filesdownloaded)
End If
Next
ftpClient.Dispose()
End Using
有没有更快的方法从 VB.NET 中的 FTP 服务器下载数百个小文件(最大 10 KB)?
如果可以选择只登录一次 FTP,而不是为每个文件登录和注销,那将是最好的。
我发现其他人有同样的问题,但没有工作结果: Using FTP to download each file *WHILE* getting the file list
我还尝试使用Parallel.For 循环进行多线程,但WebClient 不适用于多线程。如果我尝试使用ftpClient.DownloadFileAsync(New Uri(path), trnsfrpth),也是一样。
【问题讨论】:
-
超时是由 FTP 服务器还是由您显示的代码中的某些内容生成的?您的代码所花费的时间与 WinSCP 执行相同工作所花费的时间相比如何?
-
您是否尝试过使用
Parallel.ForEach并在每个任务中创建一个新的WebClient?
标签: vb.net multithreading ftp webclient