Imports System.Net Imports System.IO Imports System.Text Imports System.Threading
Public Class FTP Public m_Error As String = Nothing Private m_Uri As String Private m_User As String Private m_Pwd As String Private m_Port As Integer Private m_LocalFolder As String Private m_File As String Private m_Files() As String Public m_Incoming As Thread Private m_ThreadAction As Thread_Action_State
Public Sub New(ByRef threadaction As Thread_Action_State) m_ThreadAction = threadaction End Sub
Public Sub Start() m_Incoming = New Thread(AddressOf DownloadFiles) m_Incoming.Priority = ThreadPriority.Normal m_Incoming.IsBackground = True m_Incoming.Start() End Sub
Public Function GetError() As String Try Return m_Error Catch ex As Exception Return ex.ToString End Try End Function
Private Sub DownloadFiles() While Not m_ThreadAction.StopThread If Not m_ThreadAction.Pause Then Try Dim bDownload As Boolean bDownload = GetFile(Me.URI, Me.File, Me.User, Me.Pwd, Me.Port, Me.LocalFolder)
If Not (bDownload) Then Throw New Exception("Failed to Download File: " + Me.File) End If Catch tab As ThreadAbortException WriteLogEvent(My.Resources.ThreadAbortMessage + "_" + tab.ToString + "_" + Now.ToString, THREAD_ABORT_ERROR, EventLogEntryType.Error, My.Resources.Source) Catch ex As Exception WriteLogEvent(My.Resources.ThreadErrorMessage + "_" + ex.ToString + "_" + Now.ToString, THREAD_ERROR, EventLogEntryType.Error, My.Resources.Source) End Try End If
If Not m_ThreadAction.StopThread Then Thread.Sleep(THREAD_WAIT) End If End While
End Sub
Private Shared Sub WriteLogEvent(ByVal pszMessage As String, ByVal dwID As Long, ByVal iType As EventLogEntryType, ByVal pszSource As String) Try Dim eLog As EventLog = New EventLog("Application") eLog.Source = pszSource
Dim eInstance As EventInstance = New EventInstance(dwID, 0, iType) Dim strArray() As String
ReDim strArray(1) strArray(0) = pszMessage eLog.WriteEvent(eInstance, strArray)
eLog.Dispose() Catch ex As Exception 'Do not Catch here as it doesn't do any good for now End Try End Sub
Public Property URI() As String Get Return m_Uri End Get Set(ByVal value As String) m_Uri = value End Set End Property
Public Property LocalFolder() As String Get Return m_LocalFolder End Get Set(ByVal value As String) m_LocalFolder = value End Set End Property
Public Property User() As String Get Return m_User End Get Set(ByVal value As String) m_User = value End Set End Property
Public Property Pwd() As String Get Return m_Pwd End Get Set(ByVal value As String) m_Pwd = value End Set End Property
Public Property Port() As Integer Get Return m_Port End Get Set(ByVal value As Integer) m_Port = value End Set End Property
Public Property File() As String Get Return m_File End Get Set(ByVal value As String) m_File = value End Set End Property
Public Property Files() As String() Get Return m_Files End Get Set(ByVal value As String()) Array.Copy(value, m_Files, value.Length) End Set End Property
Public Function GetFile( _ ByVal pszUri As String, _ ByVal pszFile As String, _ ByVal pszUser As String, _ ByVal pszPWD As String, _ ByVal dwPort As Integer, _ ByVal pszLocalDir As String) _ As Boolean Try ' Set up the request Dim ftpURI As New Uri(pszUri + "/" + pszFile)
Dim ftpRequest As FtpWebRequest = _ CType(FtpWebRequest.Create(ftpURI), FtpWebRequest) 'Setup the Credentials If pszUser.ToUpper <> "ANONYMOUS" Then ftpRequest.Credentials = New NetworkCredential(pszUser, pszPWD) End If
'Download a file. ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile
' get the response object Dim ftpResponse As FtpWebResponse = _ CType(ftpRequest.GetResponse, FtpWebResponse)
Dim tmpStream As Stream = Nothing Dim tmpReader As StreamReader = Nothing Dim tmpWriter As StreamWriter = Nothing
'Read the Stream from the Response and Save the File Locally Try tmpStream = ftpResponse.GetResponseStream tmpReader = New StreamReader(tmpStream, Encoding.UTF8) tmpWriter = New StreamWriter(pszLocalDir + "\" + pszFile, False) tmpWriter.Write(tmpReader.ReadToEnd) Finally tmpStream.Close() tmpReader.Close() tmpWriter.Close() End Try
Return True Catch ex As Exception m_Error = ex.ToString Return False End Try
End Function
Public Function GetFiles( _ ByVal pszUri As String, _ ByVal pszFiles() As String, _ ByVal pszUser As String, _ ByVal pszPWD As String, _ ByVal dwPort As Integer, _ ByVal pszLocalDir As String) _ As Boolean() Try 'Setup our Return Object Dim bRet(pszFiles.Length - 1) As Boolean
Dim iLoop As Short
For iLoop = 0 To pszFiles.Length - 1 Try bRet(iLoop) = GetFile( _ pszUri, _ pszFiles(iLoop), _ pszUser, _ pszPWD, _ dwPort, _ pszLocalDir _ ) Catch ex As Exception bRet(iLoop) = False End Try Next
Return bRet Catch ex As Exception m_Error = ex.ToString Return Nothing End Try
End Function
Public Function SendFile( _ ByVal pszServer As String, _ ByVal pszRemoteDir As String, _ ByVal pszFile As String, _ ByVal pszUser As String, _ ByVal pszPWD As String, _ ByVal dwPort As Integer, _ ByVal pszLocalDir As String) _ As Boolean Try ' Set up the request Dim ftpURI As New Uri("FTP://" + pszServer + "/" + pszRemoteDir + "/" + pszFile)
Dim ftpRequest As FtpWebRequest = _ CType(FtpWebRequest.Create(ftpURI), FtpWebRequest)
'Setup the Credentials ftpRequest.Credentials = New NetworkCredential(pszUser, pszPWD)
'Upload a file. 'setup options ftpRequest.Method = WebRequestMethods.Ftp.UploadFile ftpRequest.UseBinary = False ' ftpRequest.UsePassive = True
'Now we have to read the local file into a stream Dim pFile As New FileInfo(pszLocalDir + pszFile) Dim bData(pFile.Length) As Byte Dim fStream As FileStream = pFile.OpenRead
fStream.Read(bData, 0, pFile.Length) fStream.Close()
ftpRequest.ContentLength = bData.Length Dim tmpStream As Stream = Nothing
'Read the Stream from the Response and Save the File Locally Try tmpStream = ftpRequest.GetRequestStream tmpStream.Write(bData, 0, bData.Length) Finally tmpStream.Close() End Try
'Whats the Response Dim ftpResponse As FtpWebResponse = _ CType(ftpRequest.GetResponse, FtpWebResponse) Return True Catch ex As Exception m_Error = ex.ToString Return False End Try End Function
Public Function SendFiles( _ ByVal pszServer As String, _ ByVal pszRemoteDir As String, _ ByVal pszFiles() As String, _ ByVal pszUser As String, _ ByVal pszPWD As String, _ ByVal dwPort As Integer, _ ByVal pszLocalDir As String) _ As Boolean() Try Dim iLoop As Short Dim bRet(pszFiles.Length - 1) As Boolean
For iLoop = 0 To pszFiles.Length - 1 Try
bRet(iLoop) = SendFile( _ pszServer, _ pszRemoteDir, _ pszFiles(iLoop), _ pszUser, _ pszPWD, _ dwPort, _ pszLocalDir _ ) Catch ex As Exception bRet(iLoop) = False End Try Next
Return bRet Catch ex As Exception m_Error = ex.ToString Return Nothing End Try End Function
Public Sub Dispose() Try Return Catch ex As Exception m_Error = ex.ToString Return End Try End Sub
Public ReadOnly Property Incoming() As Thread Get Return m_Incoming End Get End Property End Class
|