FTP download txt files

kasteel

Well-known member
Joined
May 29, 2009
Messages
50
Programming Experience
10+
Hi

I am using the code below to download a text file called readme.txt from the httpdocs folder using FTP.

How can I download multiple txt files in the folder without knowing what their names would be? Would appreciate any help.

Here is the code I use to download one file:

VB.NET:
Imports System
Imports System.Net
Imports System.Data
Imports System.IO

Public Class Form1
    Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click

        Const localFile As String = "C:\test1\Readme.txt"
        Const remoteFile As String = "/httpdocs/readme.txt"
        Const host As String = "ftp://yourlinkhere"
        Const username As String = "yourusernamehere"
        Const password As String = "yourpasswordhere"

        Dim URI As String = host & remoteFile
        Dim ftp As System.Net.FtpWebRequest = _
            CType(FtpWebRequest.Create(URI), FtpWebRequest)

        ftp.Credentials = New _
            System.Net.NetworkCredential(username, password)

        ftp.KeepAlive = False

        ftp.UseBinary = True

        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

                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
                    responseStream.Close()
                    fs.Flush()
                    fs.Close()
                End Using
                responseStream.Close()
            End Using
            response.Close()
        End Using

    End Sub

End Class
 
If its bombing out at the FtpDownload function, you apparently already connected to the ftp directory for the listdirectory function. To download a file, of course you have to specify each of the parameters in the function the same as in my example where i am calling the function.
 
Sorry I was kinda busy today at work today and rushing thru my answers. Did you figure it out or do you still need help?

The FtpDownloadFile function has 6 paramaters to be passed to it.

01) strUrl : the ftp full path to the directory you want. So if your login takes you to a root directory and you have to drill down a few subdirectories, you want to put that whole path in.

02) strFileName : is the ftp file you want to download. If you look at my calling sub (btnDownload) in the loop each itteration of "objFile.ToString" will give the file name.

03) strTargetDirectory : is the directory path you want to download the file too

04) strTargetFileName : is the file name you want to save it as. Unless your changing its name, you can just use objFile.ToString to keep it the same.

05 & 06) Login & password : self explanatory.

If your still having problems, post an example of how your changes. You can alter that loop a bit to add the filenames to a listbox so you can see a list of what is being returned. Just add ListBox1.Items.Add(objFile.ToString) in the loop before the download function.
 
Back
Top