Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Need FTP Class
Message
From
16/06/2011 23:35:22
 
General information
Forum:
ASP.NET
Category:
Third party products
Title:
Miscellaneous
Thread ID:
01514847
Message ID:
01514853
Views:
78
>Anyone have an FTP class? I would prefer the source code along with it.

Here is an overview. The core comes from my framework but this should give you a head start:
Imports System.IO

Namespace Framework

    Public Class FTP

        ' This is used for specific FTP server setup where we need to use the drive assignation
        ' in order to transfer a file
        Public cDrive As String = ""

        Public cHost As String = ""
        Public cLocalFile As String = ""
        Public cMessage As String = ""
        Public cPassword As String = ""
        Public cRemoteDirectory As String = ""
        Public cRemoteFile As String = ""
        Public cRemoteFileForTheRenameOfTheFile As String = ""
        Public cUsername As String = ""
        Public lFileExist As Boolean = False
        Public nBuffer As Integer = 4096

        ' Number of files in the directory
        Public nCount As Integer = 0

        Public nSecond As Integer = 0

        ' Server type
        ' 1 - FTP
        ' 2 - FTP/SSL (AUTH SSL)
        ' 3 - FTPS/Implicit SSL
        ' 4 - HTTP
        ' 5 - HTTPS
        ' 6 - SFTP/SSH
        Public nNoFTPProtocol As Integer = 1

        Public nSize As Integer = 0
        Public nPort As Integer = 21

        ' Directory collection
        Public oDirectory As Collection = New Collection

        ' FileDirectory object
        Public oFileDirectory As Framework.FileDirectory

        ' Originator
        Public oOriginator As System.Windows.Forms.Form = Nothing

        Private cDownloading As String = ""
        Private cOf As String = ""
        Private cRemaining As String = ""
        Private cTo As String = ""
        Private cTheFTPDirectoryDoesNotExist As String = ""
        Private cTheLocalFileDoesNotExist As String = ""
        Private cTheRemoteFileDoesNotExist As String = ""
        Private cTransferInProgress As String = ""
        Private cUnableToCloseTheConnection As String = ""
        Private cUnableToCreateLocalFile As String = ""
        Private cUnableToDeleteTheFile As String = ""
        Private cUnableToGetAHandle As String = ""
        Private cUnableToInitialize As String = ""
        Private cUnableToRenameTheFile As String = ""
        Private cUploading As String = ""
        Private nFTPSession As Integer = 0
        Private nNoLanguage As Integer = 0
        Private nSession As Integer = 0
        Private oApp As Framework.App
        Private oProcess As Framework.LXProcess

        ' This is when we access the class in a desktop mode
        Sub New(ByVal toApplication As Framework.App)
            oApp = toApplication
            nNoLanguage = oApp.nLanguage
            Init()
        End Sub

        ' This is when we access the class in a Web or Web Service mode
        Public Sub New(ByVal toProcess As Framework.LXProcess)
            oProcess = toProcess
            oApp = oProcess.oApp
            nNoLanguage = oProcess.nLanguage
            Init()
        End Sub

        Private Function Init() As Boolean

            ' Based on the language
            Select Case nNoLanguage

                ' English
                Case 1
                    cDownloading = "Downloading"
                    cOf = "of"
                    cRemaining = "Remaining"
                    cTo = "to"
                    cTheFTPDirectoryDoesNotExist = "The FTP directory /""##cRemoteDirectory##"" does not exist."
                    cTheLocalFileDoesNotExist = "The local file does not exist."
                    cTheRemoteFileDoesNotExist = "The remote file does not exist."
                    cTransferInProgress = "Transfer in progress"
                    cUnableToCloseTheConnection = "Unable to close the connection."
                    cUnableToCreateLocalFile = "Unable to create the local file."
                    cUnableToDeleteTheFile = "Unable to delete the file"
                    cUnableToGetAHandle = "Unable to get a handle on the FTP object."
                    cUnableToInitialize = "Unable to initialize the FTP object."
                    cUnableToRenameTheFile = "Unable to rename to file"
                    cUploading = "Uploading"

                    ' French
                Case 2
                    cDownloading = "Téléchargement"
                    cOf = "de"
                    cRemaining = "Restant"
                    cTo = "à"
                    cTheFTPDirectoryDoesNotExist = "Le répertoire /""##cRemoteDirectory##"" n'existe pas."
                    cTheLocalFileDoesNotExist = "The local file does not exist."
                    cTheRemoteFileDoesNotExist = "Le fichier local n'existe pas."
                    cTransferInProgress = "Transfert en cours"
                    cUnableToCloseTheConnection = "Impossible de fermer la connexion."
                    cUnableToCreateLocalFile = "Impossible de créer le fichier local."
                    cUnableToDeleteTheFile = "Impossible d'enlever le fichier"
                    cUnableToGetAHandle = "Impossible d'obtenir un lien avec l'objet FTP."
                    cUnableToInitialize = "Impossible d'initialiser l'objet FTP."
                    cUnableToRenameTheFile = "Impossible de renommer le fichier"
                    cUploading = "Téléchargement"

                    ' Spanish
                Case 3
                    cDownloading = "Downloading"
                    cOf = "of"
                    cRemaining = "Remaining"
                    cTo = "to"
                    cTheFTPDirectoryDoesNotExist = "The FTP directory /""##cRemoteDirectory##"" does not exist."
                    cTheLocalFileDoesNotExist = "The local file does not exist."
                    cTheRemoteFileDoesNotExist = "The remote file does not exist."
                    cTransferInProgress = "Transfer in progress"
                    cUnableToCloseTheConnection = "Unable to close the connection."
                    cUnableToCreateLocalFile = "Unable to create the local file."
                    cUnableToDeleteTheFile = "Unable to delete the file"
                    cUnableToGetAHandle = "Unable to get a handle on the FTP object."
                    cUnableToInitialize = "Unable to initialize the FTP object."
                    cUnableToRenameTheFile = "Unable to rename to file"
                    cUploading = "Uploading"

                    ' Portuguese
                Case 4
                    cDownloading = "Downloading"
                    cOf = "of"
                    cRemaining = "Remaining"
                    cTo = "to"
                    cTheFTPDirectoryDoesNotExist = "The FTP directory /""##cRemoteDirectory##"" does not exist."
                    cTheLocalFileDoesNotExist = "The local file does not exist."
                    cTheRemoteFileDoesNotExist = "The remote file does not exist."
                    cTransferInProgress = "Transfer in progress"
                    cUnableToCloseTheConnection = "Unable to close the connection."
                    cUnableToCreateLocalFile = "Unable to create the local file."
                    cUnableToDeleteTheFile = "Unable to delete the file"
                    cUnableToGetAHandle = "Unable to get a handle on the FTP object."
                    cUnableToInitialize = "Unable to initialize the FTP object."
                    cUnableToRenameTheFile = "Unable to rename to file"
                    cUploading = "Uploading"

            End Select

            Return True
        End Function

        ' Download a file
        Public Function Download() As Boolean
            Dim lcMinute As String = ""
            Dim lcPort As String = ""
            Dim lcServerType As String = ""
            Dim lcURI As String = ""
            Dim llShowAsPerPercentage As Boolean = True
            Dim llSuccess As Boolean = False
            Dim lnActual As Long = 0
            Dim lnActualABS As Long = 0
            Dim lnBytesRead As Integer = 0
            Dim lnContentLength As Long = 0
            Dim lnElapse As Integer = 0
            Dim lnLast As Integer = 0
            Dim lnPercentage As Integer = 0
            Dim lnDownloaded As Long = 0
            Dim lnRemaining As Long = 0
            Dim lnSecond As Integer = 0
            Dim lnStart As Double = Date.Now.Ticks
            Dim loBuffer(nBuffer) As Byte
            Dim loFileStream As FileStream = Nothing
            Dim loFtpWebRequest As System.Net.FtpWebRequest = Nothing
            Dim loFtpWebResponse As System.Net.FtpWebResponse = Nothing
            Dim loProgressBar As Framework.ProgressBar = New Framework.ProgressBar(oApp)
            Dim loStream As Stream = Nothing
            Dim loStreamReader As StreamReader = Nothing

            ' Reset the values
            cMessage = ""
            nSize = 0

            ' Initialization
            lcServerType = GetServerType(nNoFTPProtocol)

            ' If the port has not been defined
            If nPort = 0 Then
                nPort = 21
            End If

            ' If we have a port different than 21
            If nPort <> 21 Then
                lcPort = ":" + nPort.ToString
            End If

            ' Trim everything
            cPassword = Trim(cPassword)
            cRemoteDirectory = Trim(cRemoteDirectory)
            cUsername = Trim(cUsername)

            Try

                ' If we have an originator
                If Not oOriginator Is Nothing Then
                    loProgressBar.oOriginator = oOriginator
                End If

                ' Initialize the progress bar
                loProgressBar.ShowProgress(0, cDownloading + " " + cLocalFile + "...")

                ' URI
                lcURI = lcServerType + "://" + cHost + lcPort + "/" + cRemoteDirectory + "/" + cRemoteFile

                ' Because there is a bug in the .NET Framework with the use of
                ' System.Net.WebRequestMethods.Ftp.DownloadFile which will always return -1,
                ' we have to use a two step process to achieve this method

                ' FTP setup
                loFtpWebRequest = System.Net.FtpWebRequest.Create(New Uri(lcURI))
                loFtpWebRequest.Credentials = New System.Net.NetworkCredential(cUsername, cPassword)
                loFtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.GetFileSize
                loFtpWebRequest.UseBinary = True

                ' If we use FTP/SSL  (AUTH SSL)
                If nNoFTPProtocol = 2 Then
                    loFtpWebRequest.EnableSsl = True
                End If

                loFtpWebResponse = loFtpWebRequest.GetResponse()

                ' Get the length
                lnContentLength = loFtpWebResponse.ContentLength

                ' Assign the the public property
                nSize = lnContentLength

                loFtpWebResponse.Close()

                ' FTP setup
                loFtpWebRequest = System.Net.FtpWebRequest.Create(New Uri(lcURI))
                loFtpWebRequest.Credentials = New System.Net.NetworkCredential(cUsername, cPassword)
                loFtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
                loFtpWebRequest.UseBinary = True
                loFtpWebRequest.UsePassive = True
                loFtpWebRequest.KeepAlive = False
                loFtpWebRequest.Timeout = 10000000
                loFtpWebRequest.ReadWriteTimeout = 10000000

                ' If we use FTP/SSL  (AUTH SSL)
                If nNoFTPProtocol = 2 Then
                    loFtpWebRequest.EnableSsl = True
                End If

                loFtpWebResponse = loFtpWebRequest.GetResponse()

                loStream = loFtpWebResponse.GetResponseStream()

                ' Create the file
                Try
                    loFileStream = New FileStream(cLocalFile, FileMode.Create)
                Catch loError As Exception
                    cMessage = cUnableToCreateLocalFile
                    Exit Try
                End Try

                ' If the content length is greater than 100MB
                If lnContentLength > 100000000 Then
                    llShowAsPerPercentage = False
                End If

                ' While there is something to download
                While True

                    ' Prevent application from crashing
                    System.Windows.Forms.Application.DoEvents()

                    ' Try to read the data
                    lnBytesRead = loStream.Read(loBuffer, 0, loBuffer.Length)

                    If lnBytesRead = 0 Then

                        ' Nothing was read, finished downloading
                        System.Windows.Forms.Application.DoEvents()

                        Exit While
                    Else

                        ' Write the downloaded data
                        loFileStream.Write(loBuffer, 0, lnBytesRead)

                        ' Increment the number of uploaded bytes
                        lnDownloaded = lnDownloaded + lnBytesRead

                        ' If we show as per the percentage
                        If llShowAsPerPercentage Then
                            lnActual = lnDownloaded / lnContentLength * 100
                            lnActualABS = Math.Abs(lnActual)

                            ' If the last is not the same as the actual
                            If lnLast <> lnActual Then

                                lnElapse = ((Date.Now.Ticks - lnStart) / 10000000)

                                lcMinute = oApp.SecondToFormat(lnElapse)

                                lnRemaining = lnContentLength - lnDownloaded

                                ' Update the progress bar
                                loProgressBar.ShowProgress(lnActual, _
                                 cDownloading + " " + cLocalFile + "..." + oApp.cCR + _
                                 oApp.GetFormatValue(lnDownloaded, ",") + " bytes " + cOf + " " + _
                                 oApp.GetFormatValue(lnContentLength, ",").ToString + " - " + cRemaining + " " + _
                                 oApp.GetFormatValue(lnRemaining, ",").ToString + " bytes - " + lcMinute)

                            Else

                                ' Update the progress bar
                                loProgressBar.ShowProgress(lnActual)

                            End If

                            lnLast = lnActualABS
                        Else
                            lnActual = lnDownloaded / lnContentLength * 100
                            lnSecond = Date.Now.Second

                            ' If the last is not the same as the actual
                            If lnLast <> lnSecond Then

                                lnElapse = ((Date.Now.Ticks - lnStart) / 10000000)

                                lcMinute = oApp.SecondToFormat(lnElapse)

                                lnRemaining = lnContentLength - lnDownloaded

                                ' Update the progress bar
                                loProgressBar.ShowProgress(lnActual, _
                                 cDownloading + " " + cLocalFile + "..." + oApp.cCR + _
                                 oApp.GetFormatValue(lnDownloaded, ",") + " bytes " + cOf + " " + _
                                 oApp.GetFormatValue(lnContentLength, ",").ToString + " - " + cRemaining + " " + _
                                 oApp.GetFormatValue(lnRemaining, ",").ToString + " bytes - " + lcMinute)

                            Else

                                ' Update the progress bar
                                loProgressBar.ShowProgress(lnActual)

                            End If

                            lnLast = lnSecond
                        End If

                    End If

                End While

                ' Elapse time it took for the download
                nSecond = lnElapse

                loStream.Close()
                loFtpWebResponse.Close()
                loFileStream.Close()

                llSuccess = True
            Catch loError As Exception
                cMessage = loError.Message + " The URI is " + lcURI + "."
            End Try

            ' Make sure to remove it
            loProgressBar.Dispose()

            ' Force a refresh
            Windows.Forms.Application.DoEvents()

            Return llSuccess
        End Function

        ' Upload a file
        Public Function Upload() As Boolean
            Dim lcDrive As String = ""
            Dim lcMinute As String = ""
            Dim lcPort As String = ""
            Dim lcServerType As String = ""
            Dim lcURI As String = ""
            Dim llSuccess As Boolean = False
            Dim lnActual As Integer = 0
            Dim lnActualABS As Integer = 0
            Dim lnContentLength As Integer = 0
            Dim lnElapse As Integer = 0
            Dim lnFileLength As Integer = 0
            Dim lnLast As Integer = 0
            Dim lnPercentage As Integer = 0
            Dim lnRemaining As Integer = 0
            Dim lnStart As Double = Date.Now.Ticks
            Dim lnUploaded As Integer = 0
            Dim loBuffer(nBuffer) As Byte
            Dim loFileStream As FileStream = Nothing
            Dim loFtpWebRequest As System.Net.FtpWebRequest = Nothing
            Dim loProgressBar As Framework.ProgressBar = New Framework.ProgressBar(oApp)
            Dim loStream As Stream = Nothing

            ' Reset the values
            cMessage = ""
            nSize = 0

            ' Initialization
            lcServerType = GetServerType(nNoFTPProtocol)

            ' If the port has not been defined
            If nPort = 0 Then
                nPort = 21
            End If

            ' If we have a port different than 21
            If nPort <> 21 Then
                lcPort = ":" + nPort.ToString
            End If

            ' Trim everything
            cHost = Trim(cHost)
            cPassword = Trim(cPassword)
            cRemoteDirectory = Trim(cRemoteDirectory)
            cUsername = Trim(cUsername)

            Try

                ' If the local file does not exist
                If Not oApp.FileExist(cLocalFile) Then
                    cMessage = cTheLocalFileDoesNotExist
                    Exit Try
                End If

                loFileStream = New FileStream(cLocalFile, FileMode.Open)

                ' Get the length of the file
                lnFileLength = loFileStream.Length

                ' Assign to the public property
                nSize = lnFileLength

                ' If we have an originator
                If Not oOriginator Is Nothing Then
                    loProgressBar.oOriginator = oOriginator
                End If

                ' Initialize the progress bar
                loProgressBar.ShowProgress(0, cUploading + " " + cLocalFile + "...")

                ' If we have a drive
                If cDrive.Length > 0 Then
                    lcDrive = cDrive + ":/"
                End If

                ' URI
                lcURI = lcServerType + "://" + cHost + lcPort + "/" + lcDrive + cRemoteDirectory + "/" + cRemoteFile

                ' FTP setup
                loFtpWebRequest = System.Net.FtpWebRequest.Create(New Uri(lcURI))
                loFtpWebRequest.Credentials = New System.Net.NetworkCredential(cUsername, cPassword)
                loFtpWebRequest.KeepAlive = False
                loFtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile
                loFtpWebRequest.UseBinary = True
                loFtpWebRequest.ContentLength = lnFileLength

                ' If we use FTP/SSL  (AUTH SSL)
                If nNoFTPProtocol = 2 Then
                    loFtpWebRequest.EnableSsl = True
                End If

                loStream = loFtpWebRequest.GetRequestStream()

                ' Read a new set of bytes
                lnContentLength = loFileStream.Read(loBuffer, 0, nBuffer)

                ' While there is something to upload
                While lnContentLength <> 0

                    ' Write to the FTP
                    loStream.Write(loBuffer, 0, lnContentLength)

                    ' Read a new set of bytes
                    lnContentLength = loFileStream.Read(loBuffer, 0, nBuffer)

                    ' Increment the number of uploaded bytes
                    lnUploaded = lnUploaded + nBuffer

                    lnActual = lnUploaded / lnFileLength * 100

                    lnActualABS = Math.Abs(lnActual)

                    ' If the last is not the same as the actual
                    If lnLast <> lnActual Then

                        lnElapse = ((Date.Now.Ticks - lnStart) / 10000000)

                        lcMinute = oApp.SecondToFormat(lnElapse)

                        lnRemaining = lnFileLength - lnUploaded

                        ' Update the progress bar
                        loProgressBar.ShowProgress(lnActual, _
                         cUploading + " " + cLocalFile + "..." + oApp.cCR + _
                         oApp.GetFormatValue(lnUploaded, ",") + " bytes " + cOf + " " + _
                         oApp.GetFormatValue(lnFileLength, ",") + " - " + cRemaining + " " + _
                         oApp.GetFormatValue(lnRemaining, ",").ToString + " bytes - " + lcMinute)

                    Else

                        ' Update the progress bar
                        loProgressBar.ShowProgress(lnActual)

                    End If

                    lnLast = lnActualABS

                End While

                ' Make sure to remove it
                loProgressBar.Dispose()

                ' Force a refresh
                Windows.Forms.Application.DoEvents()

                ' Elapse time it took for the upload
                nSecond = lnElapse

                loStream.Close()
                loFileStream.Close()

                llSuccess = True
            Catch loError As Exception
                cMessage = loError.Message + " The URI is " + lcURI + "."

                ' If we have a stream
                If Not loStream Is Nothing Then
                    loStream.Close()
                End If

                ' If we have a file stream
                If Not loFileStream Is Nothing Then
                    loFileStream.Close()
                End If

                ' Make sure to remove it
                loProgressBar.Dispose()

                ' Force a refresh
                Windows.Forms.Application.DoEvents()

            End Try

            Return llSuccess
        End Function

        ' Delete a file
        Public Function DeleteFile() As Boolean
            Dim lcDrive As String = ""
            Dim lcPort As String = ""
            Dim lcServerType As String = ""
            Dim lcURI As String = ""
            Dim llSuccess As Boolean = False
            Dim loFtpWebRequest As System.Net.FtpWebRequest = Nothing

            ' Initialization
            lcServerType = GetServerType(nNoFTPProtocol)

            ' Trim everything
            cPassword = Trim(cPassword)
            cRemoteDirectory = Trim(cRemoteDirectory)
            cRemoteFile = Trim(cRemoteFile)
            cUsername = Trim(cUsername)

            ' If we have a drive
            If cDrive.Length > 0 Then
                lcDrive = cDrive + ":/"
            End If

            ' If the port has not been defined
            If nPort = 0 Then
                nPort = 21
            End If

            ' If we have a port different than 21
            If nPort <> 21 Then
                lcPort = ":" + nPort.ToString
            End If

            ' URI
            lcURI = lcServerType + "://" + cHost + lcPort + "/" + lcDrive + cRemoteDirectory + "/" + cRemoteFile

            ' If the FileExist method fails
            If Not FileExist() Then
                Return False
            End If

            ' If the file exist
            If lFileExist Then

                Try

                    ' FTP setup
                    loFtpWebRequest = System.Net.FtpWebRequest.Create(New Uri(lcURI))
                    loFtpWebRequest.Credentials = New System.Net.NetworkCredential(cUsername, cPassword)
                    loFtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.DeleteFile

                    ' If we use FTP/SSL  (AUTH SSL)
                    If nNoFTPProtocol = 2 Then
                        loFtpWebRequest.EnableSsl = True
                    End If

                    ' Delete the file
                    loFtpWebRequest.GetResponse().Close()

                    llSuccess = True
                Catch loError As Exception
                    cMessage = cUnableToDeleteTheFile + " " + lcURI + "." + oApp.cCR + oApp.cCR + loError.Message
                End Try

            Else
                llSuccess = True
            End If

            Return llSuccess
        End Function

        ' Rename a file
        Public Function RenameFile() As Boolean
            Dim lcDrive As String = ""
            Dim lcFile2 As String = ""
            Dim lcPort As String = ""
            Dim lcServerType As String = ""
            Dim lcURI As String = ""
            Dim lcURI2 As String = ""
            Dim llSuccess As Boolean = False
            Dim lnResult As Integer = 0
            Dim loFtpWebRequest As System.Net.FtpWebRequest = Nothing

            ' Initialization
            lcServerType = GetServerType(nNoFTPProtocol)

            ' If we have a drive
            If cDrive.Length > 0 Then
                lcDrive = cDrive + ":/"
            End If

            ' Trim everything
            cPassword = Trim(cPassword)
            cRemoteDirectory = Trim(cRemoteDirectory)
            cUsername = Trim(cUsername)

            ' If the port has not been defined
            If nPort = 0 Then
                nPort = 21
            End If

            ' If we have a port different than 21
            If nPort <> 21 Then
                lcPort = ":" + nPort.ToString
            End If

            ' URI
            lcURI = lcServerType + "://" + cHost + lcPort + "/" + lcDrive + cRemoteDirectory + "/" + cRemoteFile

            ' URI
            lcURI2 = cRemoteFileForTheRenameOfTheFile

            Try

                ' FTP setup
                loFtpWebRequest = System.Net.FtpWebRequest.Create(New Uri(lcURI))
                loFtpWebRequest.Credentials = New System.Net.NetworkCredential(cUsername, cPassword)
                loFtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.Rename

                ' If we use FTP/SSL  (AUTH SSL)
                If nNoFTPProtocol = 2 Then
                    loFtpWebRequest.EnableSsl = True
                End If

                ' The new name
                loFtpWebRequest.RenameTo = lcURI2

                ' Rename the file
                loFtpWebRequest.GetResponse().Close()

                llSuccess = True
            Catch loError As Exception
                cMessage = cUnableToRenameTheFile + " " + lcURI + " " + cTo + " " + lcURI2 + "." + _
                 oApp.cCR + oApp.cCR + loError.Message
            End Try

            Return llSuccess
        End Function

        ' Detect for file existance
        Public Function FileExist() As Boolean
            Dim lcDrive As String = ""
            Dim lcPort As String = ""
            Dim lcServerType As String = ""
            Dim lcURI As String = ""
            Dim loRowDataView As DataRowView = Nothing

            ' Initialization
            lcServerType = GetServerType(nNoFTPProtocol)

            ' Trim everything
            cRemoteDirectory = Trim(cRemoteDirectory)
            cRemoteFile = Trim(cRemoteFile)

            ' Reset the values
            lFileExist = False

            ' If we have a drive
            If cDrive.Length > 0 Then
                lcDrive = cDrive + ":/"
            End If

            ' If the port has not been defined
            If nPort = 0 Then
                nPort = 21
            End If

            ' If we have a port different than 21
            If nPort <> 21 Then
                lcPort = ":" + nPort.ToString
            End If

            ' URI
            lcURI = lcServerType + "://" + cHost + lcPort + "/" + lcDrive + cRemoteDirectory + "/" + cRemoteFile

            ' Get the directory
            If Not GetDirectory() Then
                Return False
            End If

            ' For each file in the directory
            For Each loRowDataView In oFileDirectory.oDataView

                ' If this is the file
                If UCase(Trim(loRowDataView("Name"))) = UCase(cRemoteFile) Then
                    lFileExist = True
                    Exit For
                End If

            Next

            Return True
        End Function

        ' Get the files from a directory
        Public Function GetDirectory() As Boolean
            Dim lcDate As String = ""
            Dim lcDirectory As String = ""
            Dim lcDrive As String = ""
            Dim lcFile As String = ""
            Dim lcHourMinute As String = ""
            Dim lcLine As String = ""
            Dim lcPort As String = ""
            Dim lcServerType As String = ""
            Dim lcURI As String = ""
            Dim ldDate As Date = Nothing
            Dim llFound As Boolean = False
            Dim llSuccess As Boolean = False
            Dim lnCounter As Integer = 0
            Dim lnDay As Integer = 0
            Dim lnHour As Integer = 0
            Dim lnMinute As Integer = 0
            Dim lnMonth As Integer = 0
            Dim lnOccurence As Integer = 0
            Dim lnSize As Integer = 0
            Dim lnYear As Integer = 0
            Dim loFtpWebRequest As System.Net.FtpWebRequest = Nothing
            Dim loFtpWebResponse As System.Net.FtpWebResponse = Nothing
            Dim loStreamReader As StreamReader = Nothing
            Dim loStringBuilder As Framework.StringBuilderFile = New Framework.StringBuilderFile(oApp)

            ' Reset the value
            oDirectory.Clear()

            ' Initialization
            lcServerType = GetServerType(nNoFTPProtocol)

            ' Create the FileDirectory object
            oFileDirectory = New Framework.FileDirectory(oApp)

            ' Trim everything
            cHost = Trim(cHost)
            cPassword = Trim(cPassword)
            cRemoteDirectory = Trim(cRemoteDirectory)
            cRemoteFile = Trim(cRemoteFile)
            cUsername = Trim(cUsername)

            ' Reset the values
            nCount = 0

            ' If the port has not been defined
            If nPort = 0 Then
                nPort = 21
            End If

            ' If we have a port different than 21
            If nPort <> 21 Then
                lcPort = ":" + nPort.ToString
            End If

            ' If we have a drive
            If cDrive.Length > 0 Then
                lcDrive = cDrive + ":/"
            End If

            ' URI
            lcURI = lcServerType + "://" + cHost + lcPort + "/" + lcDrive + cRemoteDirectory

            Try

                ' FTP setup
                loFtpWebRequest = System.Net.FtpWebRequest.Create(New Uri(lcURI))
                loFtpWebRequest.Credentials = New System.Net.NetworkCredential(cUsername, cPassword)
                loFtpWebRequest.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails

                ' If we use FTP/SSL  (AUTH SSL)
                If nNoFTPProtocol = 2 Then
                    loFtpWebRequest.EnableSsl = True
                End If

                loFtpWebResponse = loFtpWebRequest.GetResponse()

                loStreamReader = New StreamReader(loFtpWebResponse.GetResponseStream())

                ' Load the files into the string builder
                loStringBuilder.LoadString(loStreamReader.ReadToEnd())

                ' For each file
                For lnCounter = 1 To loStringBuilder.nLine

                    ' Get the path
                    If Not loStringBuilder.MLine(lnCounter) Then
                        Exit Try
                    End If

                    ' Get the line
                    lcLine = loStringBuilder.cLine

                    ' If this is a file
                    If lcLine.StartsWith("-") Then
                        lcFile = Mid(lcLine, 56)
                        lnSize = Mid(lcLine, 30, 12)
                        lcDate = Mid(lcLine, 43, 12)

                        ' Month
                        Select Case Mid(lcDate, 1, 3)

                            ' January
                            Case "Jan"
                                lnMonth = 1

                                ' February
                            Case "Feb"
                                lnMonth = 2

                                ' March
                            Case "Mar"
                                lnMonth = 3

                                ' April
                            Case "Apr"
                                lnMonth = 4

                                ' May
                            Case "May"
                                lnMonth = 5

                                ' June
                            Case "Jun"
                                lnMonth = 6

                                ' July
                            Case "Jul"
                                lnMonth = 7

                                ' August
                            Case "Aug"
                                lnMonth = 8

                                ' September
                            Case "Sep"
                                lnMonth = 9

                                ' October
                            Case "Oct"
                                lnMonth = 10

                                ' November
                            Case "Nov"
                                lnMonth = 11

                                ' Decmeber
                            Case "Dec"
                                lnMonth = 12

                        End Select

                        lnDay = Val(Mid(lcDate, 5, 2))

                        ' Get the hour minute portion
                        lcHourMinute = Mid(lcDate, 8, 5)

                        ' If this is a hour minute representation
                        If oApp.At(":", lcHourMinute) > 0 Then
                            lnHour = Val(Mid(lcDate, 8, 2))
                            lnMinute = Val(Mid(lcDate, 11, 2))
                            lnYear = Date.Now.Year
                        Else
                            lnHour = 0
                            lnMinute = 0
                            lnYear = Val(Mid(lcDate, 8, 5))
                        End If

                        ldDate = New Date(lnYear, lnMonth, lnDay, lnHour, lnMinute, 0)

                        nCount = nCount + 1

                        ' Add the file into the loFileDirectory object
                        If Not oFileDirectory.AddFile(lcFile, lnSize, ldDate) Then
                            cMessage = ""
                            Exit Try
                        End If

                    Else

                        ' Get the directory name
                        lcDirectory = Mid(lcLine, 56)

                        ' If we have a value
                        If lcDirectory.Length > 0 Then

                            ' If this is not .
                            If lcDirectory <> "." Then

                                ' If this is not ..
                                If lcDirectory <> ".." Then

                                    ' Add the directory into the collection
                                    AddDirectory(lcDirectory)

                                End If

                            End If

                        End If

                    End If

                Next

                llSuccess = True
            Catch loError As Exception
                cMessage = loError.Message + " The URI is " + lcURI + "."
            End Try

            Return llSuccess
        End Function

        ' Add an item in the oDirectory collection
        ' expC1 Directory
        Private Function AddDirectory(ByVal tcDirectory As String) As Boolean
            Dim loObject(1) As Object

            loObject(1) = tcDirectory

            oDirectory.Add(loObject)
            Return True
        End Function

        ' Get the server type
        ' expN1 Server type
        Private Function GetServerType(ByVal tnNoFTPProtocol As Integer) As String
            Dim lcServerType As String = ""

            ' Initialization
            lcServerType = "ftp"

            ' Based on the server type
            Select Case tnNoFTPProtocol

                ' FTP
                Case 1
                    lcServerType = "ftp"

                    ' FTP/SSL (AUTH SSL)
                Case 2
                    lcServerType = "ftps"

                    ' FTPS/Implicit SSL
                Case 3
                    lcServerType = "ftps"

                    ' HTTP
                Case 4
                    lcServerType = "http"

                    ' HTTPS
                Case 5
                    lcServerType = "https"

                    ' SFTP/SSH
                Case 6
                    lcServerType = "sftp"

            End Select

            Return lcServerType
        End Function

    End Class

End Namespace
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform