End If
'Check to see if the file exists before the upload.
bFileNotFound = False
If (File.Exists(sFileName)) Then
' Open the input stream to read the source file.
input = New FileStream(sFileName, FileMode.Open)
If (offset <> 0) Then
input.Seek(offset, SeekOrigin.Begin)
End If
'Upload the file.
m_iBytes = input.Read(m_aBuffer, 0, m_aBuffer.Length)
Do While (m_iBytes > 0)
cSocket.Send(m_aBuffer, m_iBytes, 0)
m_iBytes = input.Read(m_aBuffer, 0, m_aBuffer.Length)
Loop
input.Close()
Else
bFileNotFound = True
End If
If (cSocket.Connected) Then
cSocket.Close()
End If
'Check the return value if the file was not found.
If (bFileNotFound) Then
MessageString = m_sReply
Throw New IOException("The file: " & sFileName & " was not found." & _
" Cannot upload the file to the FTP site.")
End If
ReadReply()
If (Not (m_iRetValue = 226 Or m_iRetValue = 250)) Then
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If
Finally
If Not IsNothing(input) Then
input.Close()
End If
If (cSocket.Connected) Then
cSocket.Close()
If (sFileName <> "") And (sFileName.IndexOf("\") <> -1) Then
Dim index As String = sFileName.LastIndexOf("\")
Dim TempFileName As String
TempFileName = sFileName.Substring(index + 1, sFileName.Length - index - 1)
DeleteFile(TempFileName)
End If
End If
End Try
End Sub
#End Region
#Region "FTP上的文件删除"
' Delete a file from the remote FTP server.
Public Function DeleteFile(ByVal sFileName As String) As Boolean
Dim bResult As Boolean
bResult = True
If (Not (m_bLoggedIn)) Then
Login()
End If
'Send an FTP command to delete a file.
SendCommand("DELE " & sFileName)
If (m_iRetValue <> 250) Then
bResult = False
MessageString = m_sReply
End If
' Return the final result.
Return bResult
End Function
#End Region
#Region "FTP上的文件重命名"
' Rename a file on the remote FTP server.
Public Function RenameFile(ByVal sOldFileName As String, _
ByVal sNewFileName As String) As Boolean
Dim bResult As Boolean
bResult = True
If (Not (m_bLoggedIn)) Then
Login()
End If
'Send an FTP command to rename a file.
SendCommand("RNFR " & sOldFileName)
If (m_iRetValue <> 350) Then
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If
'Send an FTP command to rename a file to a file name.
'It will overwrite if newFileName exists.
SendCommand("RNTO " & sNewFileName)
If (m_iRetValue <> 250) Then
MessageString = m_sReply
Throw New IOException(m_sReply.Substring(4))
End If
' Return the final result.
Return bResult
End Function
#End Region
#Region "FTP文件夹操作"
'This is a function that is used to create a folder on the remote FTP server.
Public Function CreateDirectory(ByVal sDirName As String) As Boolean
Dim bResult As Boolean
bResult = True
If (Not (m_bLoggedIn)) Then
Login()
End If
'Send an FTP command to make a folder on the FTP server.
SendCommand("MKD " & sDirName)
If (m_iRetValue <> 257) Then
bResult = False
MessageString = m_sReply
End If
' Return the final result.
Return bResult
End Function
' This is a function that is used to delete a folder on the remote FTP server.
Public Function RemoveDirectory(ByVal sDirName As String) As Boolean
Dim bResult As Boolean
bResult = True
'Check if you are logged on to the FTP server.