我有一台FTP服务器,我想从中下载本地目录中不存在的所有文件。
我尝试过For Next
,但我无法理解它。我尝试枚举文件,但由于这两个列表,我得到一个错误。我认为错误可能是由于使用本地列表中的单个枚举文件交叉检查在线文件引起的。我该如何消除这个问题?
链接到FTPClient类代码:
https://docs.google.com/file/d/0BxFwEuHe1g77TEw2ckZxVUlQdGM/edit?usp=sharing
所有代码:
Dim ftp As New FTPclient("ftp://www.ahpg.zxq.net", "eg", "eg")
Dim dirList As FTPdirectory = ftp.ListDirectoryDetail("/")
Dim result As List(Of String) = ftp.ListDirectory("/")
For Each line As String In result
FTPLBX.Items.Add(line)
Next
Dim str As String
Dim locstr As String
Dim res_numer As IEnumerator
res_numer = result.GetEnumerator()
Dim loclist As List(Of String) = New List(Of String) _
(System.IO.Directory.EnumerateFiles("C:/Program Files/Business Elements/Recent Files"))
Dim LOC_Enum As IEnumerator
LOC_Enum = loclist.GetEnumerator
Do While LOC_Enum.MoveNext
locstr = (LOC_Enum.Current)
Loop
Do While (res_numer.MoveNext)
str = (res_numer.Current)
Loop
For Each str In loclist
If Not loclist.Contains(str) = True Then
My.Computer.Network.DownloadFile("ftp://www.ahpg.zxq.net/ftpfiles/" & str.ToString, _
"C:/Program Files/Business Elements/Recent Files/" & str.ToString, "eg", "eg")
MessageBox.Show("Done ")
End If
Next
End Sub
答案 0 :(得分:1)
如果它适合你,我会让它变得更容易一些。你走了:
' Your instance of FTPClient
Dim ftp As New FTPclient("ftp://www.ahpg.zxq.net", "eg", "eg")
' The path to destination folder (Local directory)
Dim localDir As String = "C:/Program Files/Business Elements/Recent Files/"
' Lists all the file in the given directory of FTP server
For Each file As FTPfileInfo In ftp.ListDirectoryDetail("/").GetFiles
Try
ftp.Download(file, localDir & file.Filename)
' The FTPClient class throws exception if the
' file already exists in destination directory
Catch e As ApplicationException
Console.WriteLine(e.Message)
End Try
Next file
注1 :我从FTPClient
下载了CodeProject
课程,但它与您在问题中提供的课程几乎相同。
注意2 :如果目标文件夹中存在该文件,FTPClient
本身会抛出异常。所以你不需要费心去比较文件。
注3 :请注意 locadDir 字符串末尾的尾随斜杠。没有它,该文件将被下载到业务元素文件夹。
答案 1 :(得分:0)
写一个子方法,让我们称它为IsExistedInLocal来检查来自ftp的文件是否已经在你的本地。如果是,请忽略并继续下一个;如果不是,请下载该文件。我假设你知道所有的细节,所以伪代码应该没问题
for each FTP_file in FTP_FilesList
if not IsExistedInLocal(FTP_file) then
download the file to local
end if
next
答案 2 :(得分:0)
在for循环中,您将获取列表中的每个条目,然后检查相同的列表是否没有该元素。你的if条件永远不会成立,永远不会到达DownloadFile语句。
For Each str In loclist
If Not loclist.Contains(str) = True Then
My.Computer.Network.DownloadFile("ftp://www.ahpg.zxq.net/ftpfiles/" & str.ToString, _
"C:/Program Files/Business Elements/Recent Files/" & str.ToString, "eg", "eg")
MessageBox.Show("Done ")
End If
Next