[高分悬赏贴500] VB.NET2005 如何用VB.NET下载FTP上的某个文件夹及文件夹中的所有文件到本地?
VB.NET2005 如何用VB.NET下载FTP上的某个文件夹及文件夹中的所有文件到本地并保持下载的文件夹中文件的目录结构和文件名一致。?
资料查了不少,没做出来。。。。。。 高手帮忙。。。。
http://topic.csdn.net/u/20090317/10/89181bba-73f2-4857-992c-337e704238ed.html
这一贴也一并结。
500分,现在一贴只能发100分,测试没问题开剩余4贴结分。
[解决办法]
这个不太难,先获取文件列表,在本地建立跟FTP一样的目录结构,然后一个个文件下载就是了。
明天有时间的话给你把代码贴上来。
[解决办法]
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的目录下载。
[解决办法]
http://www.west263.com/www/info/32993-1.htm
[解决办法]
用自带的FtpWebRequest 类试试,流程可以参考三楼说的,先得到一个列表,然后根据列表下载文件
以下是关于FtpWebRequest 类的MSDN参考
http://msdn.microsoft.com/zh-cn/library/system.net.ftpwebrequest(VS.80).aspx
[解决办法]
下面的地址是一个高手写的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
[解决办法]
参考
参考