Login()
End If
cSocket = CreateDataSocket()
'Send an FTP command,
SendCommand("NLST " & sMask)
If (Not (m_iRetValue = 150 Or m_iRetValue = 125)) Then
'output.Close()
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)
For i = 0 To mess.Length - 1
If mess(i) <> "" Then
mess(i) = Microsoft.VisualBasic.Left(mess(i), Len(mess(i)) - 1)
End If
Next i
cSocket.Close()
ReadReply()
If (m_iRetValue <> 226) Then
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If
Return mess
End Function
#End Region
#Region "取得文件大小"
' 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
#End Region
#Region "FTP登录联结Log on"
'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 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 folder to the
'remote FTP folder that is mapped.
ChangeDirectory(m_sRemotePath)
'Return the final result.
Return m_bLoggedIn
End Function
#End Region
#Region "BinaryMode"
'If the value of mode is true, set the 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 a 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
#End Region
#Region "FTP文件下载"
' Download a file to the local folder of the a