Imports System.IO
Imports System.Net
Imports System.Text
Public Class SimpleFTPClient
Public Sub Download(ByVal destinationFile As String, ByVal downloadUri As Uri)
Try
' Check if the URI is and FTP site
If Not (downloadUri.Scheme = Uri.UriSchemeFtp) Then
Throw New ArgumentException("URI is not an FTp site")
End If
' Set up the request
Dim ftpRequest As FtpWebRequest = CType(WebRequest.Create(downloadUri), FtpWebRequest)
' use the provided credentials
ftpRequest.Proxy = New WebProxy() 'disable proxy
ftpRequest.Credentials = New NetworkCredential(Form1.userName.Text, Form1.password.Text)
' Download a file. Look at the other methods to see all of the potential FTP features
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile
' get the response object
Dim ftpResponse As FtpWebResponse = CType(ftpRequest.GetResponse, FtpWebResponse)
Dim stream As Stream = Nothing
Dim outputStream As FileStream
Try
stream = ftpResponse.GetResponseStream
outputStream = New FileStream(destinationFile, FileMode.Create)
stream = ftpResponse.GetResponseStream
Dim cl As Long
Dim bufferSize, readCount As Integer
bufferSize = 2048
cl = ftpResponse.ContentLength
Dim buffer(bufferSize) As Byte
readCount = stream.Read(buffer, 0, bufferSize)
While readCount > 0
outputStream.Write(buffer, 0, readCount)
readCount = stream.Read(buffer, 0, bufferSize)
End While
Finally
' Allways close all streams
stream.Close()
outputStream.Close()
End Try
Catch ex As Exception
Throw ex
End Try
End Sub
Public Property UserName() As String
Get
Return Me.m_userName
End Get
Set(ByVal value As String)
Me.m_userName = value
End Set
End Property
Public Property Password() As String
Get
Return Me.m_password
End Get
Set(ByVal value As String)
Me.m_password = value
End Set
End Property
Private m_userName As String
Private m_password As String
End Class