Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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
Posted
Updated 2-Nov-17 7:05am
Comments
Richard MacCutchan 2-Nov-17 9:12am    
You cannot download folders in this way, you must download each file in turn.
CodingIsDreamy 2-Nov-17 11:56am    
thanks dude
CodingIsDreamy 2-Nov-17 11:57am    
do you by any chance know how to read all text from multiple files My.Computer.FileSystem.ReadAllText(textfile.txt) by for more then one. Sorry for asking i've just seen you have awnserd a few things that have helped me any just wondering if you new
phil.o 2-Nov-17 10:56am    
You have to retrieve the list of the files on the ftp server first; then iterate over this list to download each file.
CodingIsDreamy 2-Nov-17 11:55am    
thanks dude

1 solution

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...

VB
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...!
 
Share this answer
 
Comments
CodingIsDreamy 2-Nov-17 13:53pm    
thanks dude
Arya Isfandiari 2-May-19 2:41am    
hey, nice solution!! thank you.
netsistemas 19-May-20 13:43pm    
IMPORTANT!! Be carefoul and put the last bar in ftp and folder paths

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900