Imports System.Net
Imports System.IO
Imports System.Text
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim fs As FileStream = Nothing
Dim fwr As FtpWebRequest = Nothing
Dim ftpUrl As New StringBuilder
Dim data(4096) As Byte ' default ftp buffer size
Dim bytesRead As Integer = 0
ftpUrl.Append("ftp://ftp.server.com:21/") 'ftp url + port
Dim ftpUser As String = "username"
Dim ftpPass As String = "password"
Dim ftpFile As String = "TEST/file.jpg" 'folder/file on server
Dim savePath As String = "c:\users\username\documents\file.jpg" ' save location
ftpUrl.Append(ftpFile)
Try
fwr = FtpWebRequest.Create(ftpUrl.ToString)
fwr.UseBinary = True
fwr.Credentials = New NetworkCredential(ftpUser, ftpPass)
fwr.Method = WebRequestMethods.Ftp.DownloadFile
fs = New FileStream(savePath, FileMode.Create, FileAccess.Write, FileShare.Read)
Do
bytesRead = fwr.GetResponse.GetResponseStream.Read(data, 0, data.Length)
fs.Write(data, 0, bytesRead)
Loop Until bytesRead = 0
Catch ex As Exception
MsgBox(ex.Message)
Finally
If fs IsNot Nothing Then
fs.Close()
fs = Nothing
End If
End Try
End Sub
End Class
|