Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Public Class FTP
- Private sUsername As String = "Anonymous"
- Private sPassword As String = ""
- Private sHost As String
- Private sPort As Integer = 21
- Private sList As FilesAndFolders
- Private sSeparator As String = "/"
- Private sCurrentPath As String = "/"
- Private sChangeFolder As String = ""
- Private WithEvents XceedFTP As Xceed.Ftp.FtpClient
- Public Event Connected(ByVal List As FilesAndFolders)
- Public Event Disconnected()
- Public Event DownloadComplete(ByVal FileName As String, ByVal RemotePath As String, ByVal Data As String, ByVal e As FileTransferStatusEventArgs)
- Private sDownloadProgress As FileTransferStatusEventArgs
- Private sRemoteFile As String
- Private sLocalFile As String
- Private sData As String
- Structure DownloadProgressValues
- Dim Minimum As Long
- Dim Maximum As Long
- Dim Percent As Integer
- End Structure
- Structure FilesAndFolders
- Dim Files As Collection
- Dim Folders As Collection
- End Structure
- Public Property Username As String
- Get
- Return sUsername
- End Get
- Set(value As String)
- sUsername = value
- End Set
- End Property
- Public Property Password As String
- Get
- Return sPassword
- End Get
- Set(value As String)
- sPassword = value
- End Set
- End Property
- Public Property Port As Integer
- Get
- Return sPort
- End Get
- Set(value As Integer)
- If sPort > 10 Or sPort < 65565 Then
- sPort = value
- End If
- End Set
- End Property
- Public Property Host As String
- Get
- Return sHost
- End Get
- Set(value As String)
- sHost = value
- End Set
- End Property
- Public ReadOnly Property CurrentPath As String
- Get
- Return sCurrentPath
- End Get
- End Property
- Public WriteOnly Property ChangeFolder()
- Set(value)
- sChangeFolder = value
- End Set
- End Property
- Public ReadOnly Property DownloadProgress As FileTransferStatusEventArgs
- Get
- Return sDownloadProgress
- End Get
- End Property
- Public ReadOnly Property Slash
- Get
- Return sSeparator
- End Get
- End Property
- ' Download Async
- Private Delegate Function DelegateDownload() As Boolean
- Private Function DoDownload() As Boolean
- If XceedFTP.Connected = True Then
- XceedFTP.Disconnect()
- End If
- XceedFTP.Connect(sHost, sPort)
- If XceedFTP.Connected = True Then
- XceedFTP.Login(sUsername, sPassword)
- End If
- sLocalFile = System.IO.Path.GetTempFileName()
- XceedFTP.ReceiveFile(sRemoteFile, sLocalFile)
- sData = System.IO.File.ReadAllText(sLocalFile)
- Return True
- End Function
- Private Sub CallbackDownload(ByVal e As IAsyncResult)
- Dim t As DelegateDownload = CType(CType(e, Runtime.Remoting.Messaging.AsyncResult).AsyncDelegate, DelegateDownload)
- t.EndInvoke(e)
- RaiseEvent DownloadComplete(sLocalFile, sRemoteFile, sData, sDownloadProgress)
- End Sub
- Public Sub Download(ByVal RemoteFileName As String)
- sRemoteFile = RemoteFileName
- Dim t As DelegateDownload = AddressOf DoDownload
- t.BeginInvoke(New AsyncCallback(AddressOf CallbackDownload), Nothing)
- End Sub
- Private Sub XceedFTP_FileTransferStatus(sender As Object, e As FileTransferStatusEventArgs) Handles XceedFTP.FileTransferStatus
- sDownloadProgress = e
- End Sub
- ' Connect Async
- Private Delegate Function DelegateConnect() As Boolean
- Private Function DoConnect() As Boolean
- If XceedFTP.Connected = True Then
- XceedFTP.Disconnect()
- End If
- XceedFTP.Connect(sHost, sPort)
- If XceedFTP.Connected = True Then
- XceedFTP.Login(sUsername, sPassword)
- sSeparator = XceedFTP.ServerFolderSeparator
- sCurrentPath = XceedFTP.GetCurrentFolder()
- If sChangeFolder <> "" And sChangeFolder <> sCurrentPath Then
- XceedFTP.ChangeCurrentFolder(sChangeFolder)
- sCurrentPath = XceedFTP.GetCurrentFolder()
- End If
- Debug.Print(sCurrentPath)
- Dim fList As FtpItemInfoList = XceedFTP.GetFolderContents()
- sList.Files = Nothing
- sList.Folders = Nothing
- sList.Files = New Collection
- sList.Folders = New Collection
- Dim lItem1 As New Telerik.WinControls.UI.ListViewDataItem("..")
- lItem1.Tag = ""
- lItem1.SubItems.Add("..")
- lItem1.SubItems.Add("")
- sList.Folders.Add(lItem1)
- For Each fItem As FtpItemInfo In fList
- If fItem.Name <> "." And fItem.Name <> ".." Then
- Dim lItem As New Telerik.WinControls.UI.ListViewDataItem(fItem.Name)
- If fItem.Type = FtpItemType.Folder Then
- lItem.Tag = ""
- lItem.SubItems.Add(fItem.Name)
- lItem.SubItems.Add(fItem.DateTime)
- sList.Folders.Add(lItem)
- Else
- lItem.SubItems.Add(fItem.Name)
- lItem.SubItems.Add(fItem.Size)
- lItem.SubItems.Add(System.IO.Path.GetExtension(fItem.Name).ToLower())
- lItem.SubItems.Add(fItem.Type.ToString)
- lItem.SubItems.Add(fItem.DateTime)
- sList.Files.Add(lItem)
- End If
- End If
- Next
- Return True
- Else
- Return False
- End If
- End Function
- Private Sub CallbackConnect(ByVal e As IAsyncResult)
- Dim t As DelegateConnect = CType(CType(e, Runtime.Remoting.Messaging.AsyncResult).AsyncDelegate, DelegateConnect)
- t.EndInvoke(e)
- RaiseEvent Connected(sList)
- End Sub
- Public Sub Connect()
- Dim t As DelegateConnect = AddressOf DoConnect
- t.BeginInvoke(New AsyncCallback(AddressOf CallbackConnect), Nothing)
- End Sub
- ' Disconnect Async
- Private Delegate Function DelegateDisconnect() As Boolean
- Private Function DoDisconnect() As Boolean
- XceedFTP.Disconnect()
- Return Not XceedFTP.Connected
- End Function
- Private Sub CallbackDisconnect(ByVal e As IAsyncResult)
- Dim t As DelegateDisconnect = CType(CType(e, Runtime.Remoting.Messaging.AsyncResult).AsyncDelegate, DelegateDisconnect)
- t.EndInvoke(e)
- RaiseEvent Disconnected()
- End Sub
- Public Sub Disconnect()
- Dim t As DelegateDisconnect = AddressOf DoDisconnect
- t.BeginInvoke(New AsyncCallback(AddressOf CallbackDisconnect), Nothing)
- End Sub
- ' Initialize Component
- Public Sub New(ByVal XceedLicense As String)
- Xceed.Ftp.Licenser.LicenseKey = XceedLicense
- XceedFTP = New Xceed.Ftp.FtpClient
- End Sub
- Public Sub New(ByVal XceedLicense As String, ByVal Hostname As String)
- Xceed.Ftp.Licenser.LicenseKey = XceedLicense
- XceedFTP = New Xceed.Ftp.FtpClient
- sHost = Hostname
- End Sub
- Public Sub New(ByVal XceedLicense As String, ByVal Hostname As String, ByVal IntPort As Integer)
- Xceed.Ftp.Licenser.LicenseKey = XceedLicense
- XceedFTP = New Xceed.Ftp.FtpClient
- sHost = Hostname
- sPort = IntPort
- End Sub
- Public Sub New(ByVal XceedLicense As String, ByVal User As String, ByVal Pass As String)
- Xceed.Ftp.Licenser.LicenseKey = XceedLicense
- XceedFTP = New Xceed.Ftp.FtpClient
- sUsername = User
- sPassword = Pass
- End Sub
- Public Sub New(ByVal XceedLicense As String, ByVal Hostname As String, ByVal User As String, ByVal Pass As String)
- Xceed.Ftp.Licenser.LicenseKey = XceedLicense
- XceedFTP = New Xceed.Ftp.FtpClient
- sHost = Hostname
- sUsername = User
- sPassword = Pass
- End Sub
- Public Sub New(ByVal XceedLicense As String, ByVal Hostname As String, ByVal IntPort As Integer, ByVal User As String, ByVal Pass As String)
- Xceed.Ftp.Licenser.LicenseKey = XceedLicense
- XceedFTP = New Xceed.Ftp.FtpClient
- sPort = IntPort
- sHost = Hostname
- sUsername = User
- sPassword = Pass
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement