1

What I have:

Dim ftploader As System.Net.FtpWebRequest =
    DirectCast(System.Net.WebRequest.Create(
        "ftp://ftp.cabbageee.host-ed.me/nim/Vardelatestmessage.txt"),
        System.Net.FtpWebRequest)

ftploader.Credentials =
    New System.Net.NetworkCredential("Insert Username here", "Insert password here")

I am trying to download this .txt file to my c: drive. I already have a connection, so how can I save that .txt file? Also, how can I upload a file? I already tried My.Computer.Network.DownloadFile, but it is only possible to download/upload once, as I have no idea of how to get rid of that connection.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
user1869088
  • 11
  • 1
  • 2
  • 4

3 Answers3

2

The most trivial way to download a binary file from an FTP server using VB.NET is using WebClient.DownloadFile:

Dim client As WebClient = New WebClient()
client.Credentials = New NetworkCredential("username", "password")
client.DownloadFile(
    "ftp://ftp.example.com/remote/path/file.zip", "C:\local\path\file.zip")

If you need a greater control, that WebClient does not offer (like TLS/SSL encryption, ascii/text transfer mode, resuming transfers, etc), use FtpWebRequest. Easy way is to just copy an FTP response stream to FileStream using Stream.CopyTo:

Dim request As FtpWebRequest =
    WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip")
request.Credentials = New NetworkCredential("username", "password")
request.Method = WebRequestMethods.Ftp.DownloadFile

Using ftpStream As Stream = request.GetResponse().GetResponseStream(),
      fileStream As Stream = File.Create("C:\local\path\file.zip")
    ftpStream.CopyTo(fileStream)
End Using

If you need to monitor a download progress, you have to copy the contents by chunks yourself:

Dim request As FtpWebRequest =
    WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip")
request.Credentials = New NetworkCredential("username", "password")
request.Method = WebRequestMethods.Ftp.DownloadFile

Using ftpStream As Stream = request.GetResponse().GetResponseStream(),
      fileStream As Stream = File.Create("C:\local\path\file.zip")
    Dim buffer As Byte() = New Byte(10240 - 1) {}
    Dim read As Integer
    Do
        read = ftpStream.Read(buffer, 0, buffer.Length)
        If read > 0 Then
            fileStream.Write(buffer, 0, read)
            Console.WriteLine("Downloaded {0} bytes", fileStream.Position)
        End If
    Loop While read > 0
End Using

For GUI progress (WinForms ProgressBar), see (C#):
FtpWebRequest FTP download with ProgressBar

If you want to download all files from a remote folder, see
How to download directories from FTP using VB.NET

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
0

You'll need to call GetResponse and then you'll have access to the response stream which will include your content, you could then write that stream into the text file you want to save.

There seems to be a pretty well fleshed out sample here (it's in C# but I think should be fairly easy to translate to VB).

Jim O'Neil
  • 23,344
  • 7
  • 42
  • 67
-1

try this instead:

 Dim myWebClient As New System.Net.WebClient
 Dim webfilename As String = "http://www.whatever.com/example.txt"
 Dim file As New System.IO.StreamReader(myWebClient.OpenRead(webfilename))

 gCurrentDataFileContents = file.ReadToEnd()

 file.Close()
 file.Dispose()
 myWebClient.Dispose()
Rob
  • 3,488
  • 3
  • 32
  • 27