[高分悬赏贴500] VB.NET2005 如何用VB.NET下载FTP上的某个文件夹及文件夹中的所有文件到本地?

qing205 2009-04-22 08:01:51
VB.NET2005 如何用VB.NET下载FTP上的某个文件夹及文件夹中的所有文件到本地并保持下载的文件夹中文件的目录结构和文件名一致。?
资料查了不少,没做出来。。。。。。 高手帮忙。。。。

http://topic.csdn.net/u/20090317/10/89181bba-73f2-4857-992c-337e704238ed.html
这一贴也一并结。


500分,现在一贴只能发100分,测试没问题开剩余4贴结分。
...全文
455 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
li_guang_hua 2009-04-28
  • 打赏
  • 举报
回复
学习啊
ai_li7758521 2009-04-27
  • 打赏
  • 举报
回复
帮顶
gzhantonio 2009-04-27
  • 打赏
  • 举报
回复
学习
wuyq11 2009-04-26
  • 打赏
  • 举报
回复
qing205 2009-04-26
  • 打赏
  • 举报
回复
我试试。
一只熊猫 2009-04-23
  • 打赏
  • 举报
回复
下面的地址是一个高手写的FTP操作类,常用的功能都有,很好用。 记得感谢这位哈。
http://blog.csdn.net/Going1981/archive/2009/04/23/4103199.aspx

类里有异步下载的方法,下载进度changed事件、Completed事件都有,真是太好用了。

下面的代码可以将指定FTP的目录及下面所有子目录复制到本地,你自己加入下载的方法吧。


'先引用上面的类,或者直接Copy到项目里 (拿来主义 =。=|| )
Private Sub CreateFloders(ByVal sourcepath As String, ByVal savepath As String)
obj.GotoDirectory(sourcepath)
Dim dirlist As FileStruct() = obj.ListDirectories '获取目录列表
Dim dir As String = ""
If sourcepath.IndexOf("\") > 0 Then
dir = savepath & sourcepath.Substring(sourcepath.LastIndexOf("\"))
Else
dir = savepath & sourcepath
End If
System.IO.Directory.CreateDirectory(dir) '在本地创建目录
For i As Integer = 0 To dirlist.Length - 1
System.IO.Directory.CreateDirectory(dir & "\" & dirlist(i).Name)
obj.GotoDirectory(dirlist(i).Name)
Dim subdirlist As FileStruct() = obj.ListDirectories '获取子目录
If subdirlist.Length > 0 Then
CreateFloders(sourcepath & "\" & dirlist(i).Name, dir) '如果有子目录,递归
obj.GotoDirectory(sourcepath) '返回上级目录
End If
Next
End Sub


调用方法:

        
Private WithEvents obj As New clsFTP
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
obj.Uri = New Uri("ftp:\\124.193.***.***:**")
obj.UserName = "myuser"
obj.Password = "************"
CreateFloders(".\test", "D:\") '将FTP主目录\test 复制到本地D:\下
'obj.DownloadFileAsync("bsmain_runtime.log", "C:\", "1.txt")
'AddHandler obj.DownloadDataCompleted, AddressOf test
End Sub



ghcery 2009-04-22
  • 打赏
  • 举报
回复
用自带的FtpWebRequest 类试试,流程可以参考三楼说的,先得到一个列表,然后根据列表下载文件

以下是关于FtpWebRequest 类的MSDN参考

http://msdn.microsoft.com/zh-cn/library/system.net.ftpwebrequest(VS.80).aspx
oyljerry 2009-04-22
  • 打赏
  • 举报
回复
http://www.west263.com/www/info/32993-1.htm
a854468521 2009-04-22
  • 打赏
  • 举报
回复
oFTP = New nBitFTP.clsFTP()第三方控件。nBit Information Technologies
oFTP.Host = "www. 1234.com"
oFTP.UserName = "1234"
oFTP.Password = "1234"
Me.ListBox1.Items.Clear()
Dim wjj As New System.Collections.ArrayList
If oFTP.connect() = True Then '创建连接
If oFTP.GetDirectoryListing("*.*") = True Then
For X As Int32 = 0 To oFTP.Directory.Count - 1
Dim Wmc As String = oFTP.Directory.Item(X + 1).FileName
If oFTP.Directory.Item(X + 1).Directory = True And Wmc <> "." And Wmc <> ".." Then
wjj.Add(Wmc)
End If
Next
End If
End If
Dim Js As Int16 = 0
While wjj.Count <> Js
Js = wjj.Count
If oFTP.connect() = True Then '创建连接
For xh As Int32 = 0 To wjj.Count - 1
If oFTP.GetDirectoryListing(wjj(xh)) = True Then
For X As Int32 = 0 To oFTP.Directory.Count - 1
Dim Wmc As String = oFTP.Directory.Item(X + 1).FileName
If oFTP.Directory.Item(X + 1).Directory = True Then
If oFTP.Directory.Item(X + 1).Directory = True And Wmc <> "." And Wmc <> ".." _
And wjj.Contains(wjj(xh) & "/" & Wmc) = False Then
wjj.Add(wjj(xh) & "/" & Wmc)

End If
End If
Next
End If
Next
End If
End While
For xh As Int32 = 0 To wjj.Count - 1
Me.ListBox1.Items.Add(wjj(xh))
If IO.Directory.Exists(Me.TextBoxsave.Text & "/" & wjj(xh)) = False Then
IO.Directory.CreateDirectory(Me.TextBoxsave.Text & "/" & wjj(xh))
End If
Dgwj(wjj(xh))
Next
Me.ToolStripProgressBar1.Visible = True
Dim Xzjs As Int32 = 0
Me.ToolStripProgressBar1.Maximum = Me.DataGridVieFile.Rows.Count - 1
Dim Xzjl As New System.Collections.SortedList
For xh As Int32 = 0 To Me.DataGridVieFile.Rows.Count - 1
Dim Wmc As String = Me.DataGridVieFile.Rows(xh).Cells(0).Value
Dim Bdwj As String = Me.TextBoxsave.Text & "/" & Wmc
Dim Wjcd As Int32 = Me.DataGridVieFile.Rows(xh).Cells(2).Value
Dim Xgsj As Date = Me.DataGridVieFile.Rows(xh).Cells(3).Value
If IO.File.Exists(Bdwj) = False Then
Xzjs += 1
Me.Label1下载数量.Text = Xzjs
Xzjl.Add(Wmc, Bdwj)
Else
If New IO.FileInfo(Bdwj).Length < Wjcd Or New IO.FileInfo(Bdwj).LastWriteTime < Xgsj Then
Xzjs += 1
Me.Label1下载数量.Text = Xzjs
Xzjl.Add(Wmc, Bdwj)

End If
End If
Me.ToolStripProgressBar1.Value = xh
Next
Me.ToolStripProgressBar1.Maximum = Xzjl.Count - 1
For xh As Int32 = 0 To Xzjl.Count - 1
Me.TextBoxurl.Text = "www.1234.com/" & Xzjl.GetKey(xh)
Try
Dim Dc As New Uri("ftp://1234.com/" & Xzjl.GetKey(xh))
My.Computer.Network.DownloadFile(Dc, _
Xzjl.GetByIndex(xh), "1234", "1234", False, 500, True, FileIO.UICancelOption.ThrowException)
Catch ex As Exception
My.Computer.FileSystem.WriteAllText(Application.StartupPath & "/失败.txt", Xzjl.GetKey(xh) & Chr(13) & Chr(10), False)
' Exit Sub
End Try
Me.ToolStripProgressBar1.Value = xh
Me.Label1下载数量.Text = "下载总数:" & Xzjs & " 剩余:" & Xzjs - xh
Next
Me.ToolStripProgressBar1.Visible = False
ToolStripStatusLabel备份.Text = "备份完成"

使用了第三方的控件:
nBitFTP.clsFTP 可以完全按照FTP的目录下载。

一只熊猫 2009-04-22
  • 打赏
  • 举报
回复
这个不太难,先获取文件列表,在本地建立跟FTP一样的目录结构,然后一个个文件下载就是了。

明天有时间的话给你把代码贴上来。
wuhuwy 2009-04-22
  • 打赏
  • 举报
回复
帮顶,学习,等待高手...
qing205 2009-04-22
  • 打赏
  • 举报
回复
保持解释下 ,保证当时下载的文件夹结构和名字一致就可以了,不做实时更行。

高手们帮个忙吧。。。

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧