Dim fs As FileStream
Dim fwr As FtpWebRequest
Dim data(4096) As Byte
Dim bytesRead As Integer = 1
Dim ftpUrl As String = "ftp://ftp.server.com:21/" 'ftp url + port
Dim ftpUser As String = "username"
Dim ftpPass As String = "password"
Dim ftpFile As String = "TEST/myfile.txt" 'folder/file on server
Dim savePath As String = "C:\myfile.txt" ' save location
ftpUrl = ftpUrl.Insert(ftpUrl.Length, ftpFile)
fwr = FtpWebRequest.Create(ftpUrl)
fwr.Credentials = New NetworkCredential(ftpUser, ftpPass)
fwr.Method = WebRequestMethods.Ftp.DownloadFile
fs = New FileStream(savePath, FileMode.Create, FileAccess.Write, FileShare.Read)
Do While bytesRead > 0
bytesRead = fwr.GetResponse.GetResponseStream.Read(data, 0, data.Length)
fs.Write(data, 0, bytesRead)
Loop
fs.Close()
fs.Dispose()
fs = Nothing
fwr = Nothing
|