' This class permits you to perform direct connections to FTP sites in Visual Basic. NET.
' The class supports the following FTP commands:
' - Upload a file
' - Download a file
' - Create a directory
' - Remove a directory
' - Change directory
' - Remove a file
' - Rename a file
' - Set the user name of the remote user
' - Set the password of the remote user
Imports System
Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Net.Sockets
'FTP Class
Public Class clsFTP
#Region "Class Variable Declarations"
Private m_sRemoteHost, m_sRemotePath, m_sRemoteUser As String
Private m_sRemotePassword, m_sMess As String
Private m_iRemotePort, m_iBytes As Int32
Private m_objClientSocket As Socket
Private m_iRetValue As Int32
Private m_bLoggedIn As Boolean
Private m_sMes, m_sReply As String
'Set the size of the packet that is used to read and to write data to the FTP server
'to the following specified size.
Public Const BLOCK_SIZE = 512
Private m_aBuffer(BLOCK_SIZE) As Byte
Private ASCII As Encoding = Encoding.ASCII
Public flag_bool As Boolean
'General variable declaration
Private m_sMessageString As String
#End Region
#Region "Class Constructors"
' Main class constructor
Public Sub New()
m_sRemoteHost = "microsoft"
m_sRemotePath = "."
m_sRemoteUser = "anonymous"
m_sRemotePassword = ""
m_sMessageString = ""
m_iRemotePort = 21
m_bLoggedIn = False
End Sub
' Parameterized constructor
Public Sub New(ByVal sRemoteHost As String, _
ByVal sRemotePath As String, _
ByVal sRemoteUser As String, _
ByVal sRemotePassword As String, _
ByVal iRemotePort As Int32)
m_sRemoteHost = sRemoteHost
m_sRemotePath = sRemotePath
m_sRemoteUser = sRemoteUser
m_sRemotePassword = sRemotePassword
m_sMessageString = ""
m_iRemotePort = 21
m_bLoggedIn = False
End Sub
#End Region
#Region "Public Properties"
'Set or Get the name of the FTP server that you want to connect to.
Public Property RemoteHostFTPServer() As String
'Get the name of the FTP server.
Get
Return m_sRemoteHost
End Get
'Set the name of the FTP server.
Set(ByVal Value As String)
m_sRemoteHost = Value
End Set
End Property
'Set or Get the FTP port number of the FTP server that you want to connect to.
Public Property RemotePort() As Int32
'Get the FTP port number.
Get
Return m_iRemotePort
End Get
'Set the FTP port number.
Set(ByVal Value As Int32)
m_iRemotePort = Value
End Set
End Property
'Set or Get the remote path of the FTP server that you want to connect to.
Public Property RemotePath() As String
'Get the remote path.
Get
Return m_sRemotePath
End Get
'Set the remote path.
Set(ByVal Value As String)
m_sRemotePath = Value
End Set
End Property
'Set the remote password of the FTP server that you want to connect to.
Public Property RemotePassword() As String
Get
Return m_sRemotePassword
End Get
Set(ByVal Value As String)
m_sRemotePassword = Value
End Set
End Property
'Set or Get the remote user of the FTP server that you want to connect to.
Public Property RemoteUser() As String
Get
Return m_sRemoteUser
End Get
Set(ByVal Value As String)
m_sRemoteUser = Value
End Set
End Property
'Set the class messagestring.
Public Property MessageString() As String
Get
Return m_sMessageString
End Get
Set(ByVal Value As String)
m_sMessageString = Value
End Set
End Property
#End Region
#Region "Public Subs and Functions"
'Return a list of files from the file system. Return these files in a string() array.
Public Function GetFileList(ByVal sMask As String) As String()
Dim cSocket As Socket
Dim bytes As Int32
Dim seperator As Char = ControlChars.Lf
Dim mess() As String
m_sMes = ""
'Check to see if you are logged on to the FTP server.
If (Not (m_bLoggedIn)) Then
Login()
End If
cSocket = CreateDataSocket()
'Send an FTP command.
SendCommand("NLST " & sMask)
If (Not (m_iRetValue = 150 Or m_iRetValue = 125)) Then
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If
m_sMes = ""
Do While (True)
m_aBuffer.Clear(m_aBuffer, 0, m_aBuffer.Length)
bytes = cSocket.Receive(m_aBuffer, m_aBuffer.Length, 0)
m_sMes += ASCII.GetString(m_aBuffer, 0, bytes)
If (bytes < m_aBuffer.Length) Then
Exit Do
End If
Loop
mess = m_sMes.Split(seperator)
cSocket.Close()
ReadReply()
If (m_iRetValue <> 226) Then
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If
Return mess
End Function
' Get the size of the file on the FTP server.
Public Function GetFileSize(ByVal sFileName As String) As Long
Dim size As Long
If (Not (m_bLoggedIn)) Then
Login()
End If
'Send an FTP command.
SendCommand("SIZE " & sFileName)
size = 0
If (m_iRetValue = 213) Then
size = Int64.Parse(m_sReply.Substring(4))
Else
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If
Return size
End Function
'Log on to the FTP server.
Public Function Login() As Boolean
m_objClientSocket = _
New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Dim ep As New IPEndPoint(Dns.Resolve(m_sRemoteHost).AddressList(0), m_iRemotePort)
Try
m_objClientSocket.Connect(ep)
Catch ex As Exception
MessageString = m_sReply
Throw New IOException("Cannot connect to the remote server")
End Try
ReadReply()
If (m_iRetValue <> 220) Then
CloseConnection()
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If
'Send an FTP command to send a user logon ID to the server.
SendCommand("USER " & m_sRemoteUser)
If (Not (m_iRetValue = 331 Or m_iRetValue = 230)) Then
Cleanup()
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If
If (m_iRetValue <> 230) Then
'Send an FTP command to send a user logon password to the server.
SendCommand("PASS " & m_sRemotePassword)
If (Not (m_iRetValue = 230 Or m_iRetValue = 202)) Then
Cleanup()
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If
End If
m_bLoggedIn = True
'Call the ChangeDirectory user-defined function to change the directory to the
'remote FTP folder that is mapped.
ChangeDirectory(m_sRemotePath)
'Return the final result.
Return m_bLoggedIn
End Function
'If the value of mode is true, set binary mode for downloads. Otherwise, set ASCII mode.
Public Sub SetBinaryMode(ByVal bMode As Boolean)
If (bMode) Then
'Send the FTP command to set the binary mode.
'(TYPE is an FTP command that is used to specify representation type.)
SendCommand("TYPE I")
Else
'Send the FTP command to set ASCII mode.
'(TYPE is an FTP command that is used to specify representation type.)
SendCommand("TYPE A")
End If
If (m_iRetValue <> 200) Then
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If
End Sub