如何从ftp VB.NET下载文件夹 [英] How do I download a folder from ftp VB.NET

查看:104
本文介绍了如何从ftp VB.NET下载文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过我用来下载文件的代码,但是当我尝试下载文件夹时它不起作用的代码 -



Ive tried the code i use to download a file but when i try and download a folder it doesn't work here's the code --

Public Sub CheckForUpdates()

        Dim localFile As String
        localFile = Application.StartupPath & "/Database/Sigs/"
        Const remoteFile As String = "/Database/Sigs/"

        Const host As String = "ftp://ip address"
        Const username As String = "my username"
        Const password As String = "my password"
        Dim URI As String = host & remoteFile



        Dim ftp As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(URI), System.Net.FtpWebRequest)
        'Set the credentials
        ftp.Credentials = New System.Net.NetworkCredential(username, password)
        'Turn off KeepAlive (will close connection on completion)
        ftp.KeepAlive = False
        'we want a binary
        ftp.UseBinary = True
        'Define the action required (in this case, download a file)
        ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile



        Using response As System.Net.FtpWebResponse = CType(ftp.GetResponse, System.Net.FtpWebResponse)
            Using responseStream As IO.Stream = response.GetResponseStream
                'loop to read & write to file
                Using fs As New IO.FileStream(localFile, IO.FileMode.Create)
                    Dim buffer(2047) As Byte
                    Dim read As Integer = 0
                    Do
                        read = responseStream.Read(buffer, 0, buffer.Length)
                        fs.Write(buffer, 0, read)
                    Loop Until read = 0 'see Note(1)
                    responseStream.Close()
                    fs.Flush()
                    fs.Close()


                End Using
                responseStream.Close()
            End Using
            response.Close()
        End Using
    End Sub





什么我试过了:



我试过,你可以看到上面使用代码获取文件但由于某种原因不起作用



What I have tried:

ive tried as you can see above to use the code for getting a file but for some reason doesn't work

推荐答案

嘿......有人已经说过你了无法真正下载文件夹,但您需要逐个下载文件。您可以从'ListDirectory'获取文件列表...尝试这可能...



Hey... As someone already stated that you cannot really download the folder as such, but you need to download the files one by one. You can get the list of the files from the 'ListDirectory'... Try this maybe...

Private Shared Sub Main(args As String())
	Dim ftpRequest As FtpWebRequest = DirectCast(WebRequest.Create("ftp://mywebsite.com/"), FtpWebRequest)
	ftpRequest.Credentials = New NetworkCredential("user345", "pass234")
	ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory
	Dim response As FtpWebResponse = DirectCast(ftpRequest.GetResponse(), FtpWebResponse)
	Dim streamReader As New StreamReader(response.GetResponseStream())
	Dim directories As New List(Of String)()

	Dim line As String = streamReader.ReadLine()
	While Not String.IsNullOrEmpty(line)
		directories.Add(line)
		line = streamReader.ReadLine()
	End While
	streamReader.Close()


	Using ftpClient As New WebClient()
		ftpClient.Credentials = New System.Net.NetworkCredential("user345", "pass234")

		For i As Integer = 0 To directories.Count - 1
			If directories(i).Contains(".") Then

				Dim path As String = "ftp://mywebsite.com/" + directories(i).ToString()
				Dim trnsfrpth As String = "D:\\Test\" + directories(i).ToString()
				ftpClient.DownloadFile(path, trnsfrpth)
			End If
		Next
	End Using
End Sub



希望它有帮助......!


Hope it helps...!


这篇关于如何从ftp VB.NET下载文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆