Advertisement
Guest User

Untitled

a guest
Oct 5th, 2012
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 9.13 KB | None | 0 0
  1.     Public Class FTP
  2.         Private sUsername As String = "Anonymous"
  3.         Private sPassword As String = ""
  4.         Private sHost As String
  5.         Private sPort As Integer = 21
  6.         Private sList As FilesAndFolders
  7.         Private sSeparator As String = "/"
  8.         Private sCurrentPath As String = "/"
  9.         Private sChangeFolder As String = ""
  10.  
  11.         Private WithEvents XceedFTP As Xceed.Ftp.FtpClient
  12.  
  13.         Public Event Connected(ByVal List As FilesAndFolders)
  14.         Public Event Disconnected()
  15.         Public Event DownloadComplete(ByVal FileName As String, ByVal RemotePath As String, ByVal Data As String, ByVal e As FileTransferStatusEventArgs)
  16.  
  17.         Private sDownloadProgress As FileTransferStatusEventArgs
  18.         Private sRemoteFile As String
  19.         Private sLocalFile As String
  20.         Private sData As String
  21.  
  22.  
  23.         Structure DownloadProgressValues
  24.             Dim Minimum As Long
  25.             Dim Maximum As Long
  26.             Dim Percent As Integer
  27.         End Structure
  28.  
  29.  
  30.         Structure FilesAndFolders
  31.             Dim Files As Collection
  32.             Dim Folders As Collection
  33.         End Structure
  34.         Public Property Username As String
  35.             Get
  36.                 Return sUsername
  37.             End Get
  38.             Set(value As String)
  39.                 sUsername = value
  40.             End Set
  41.         End Property
  42.         Public Property Password As String
  43.             Get
  44.                 Return sPassword
  45.             End Get
  46.             Set(value As String)
  47.                 sPassword = value
  48.             End Set
  49.         End Property
  50.         Public Property Port As Integer
  51.             Get
  52.                 Return sPort
  53.             End Get
  54.             Set(value As Integer)
  55.                 If sPort > 10 Or sPort < 65565 Then
  56.                     sPort = value
  57.                 End If
  58.             End Set
  59.         End Property
  60.         Public Property Host As String
  61.             Get
  62.                 Return sHost
  63.             End Get
  64.             Set(value As String)
  65.                 sHost = value
  66.             End Set
  67.         End Property
  68.         Public ReadOnly Property CurrentPath As String
  69.             Get
  70.                 Return sCurrentPath
  71.             End Get
  72.         End Property
  73.         Public WriteOnly Property ChangeFolder()
  74.             Set(value)
  75.                 sChangeFolder = value
  76.             End Set
  77.         End Property
  78.         Public ReadOnly Property DownloadProgress As FileTransferStatusEventArgs
  79.             Get
  80.                 Return sDownloadProgress
  81.             End Get
  82.         End Property
  83.         Public ReadOnly Property Slash
  84.             Get
  85.                 Return sSeparator
  86.             End Get
  87.         End Property
  88.  
  89.  
  90.         ' Download Async
  91.         Private Delegate Function DelegateDownload() As Boolean
  92.         Private Function DoDownload() As Boolean
  93.             If XceedFTP.Connected = True Then
  94.                 XceedFTP.Disconnect()
  95.             End If
  96.             XceedFTP.Connect(sHost, sPort)
  97.             If XceedFTP.Connected = True Then
  98.                 XceedFTP.Login(sUsername, sPassword)
  99.             End If
  100.  
  101.             sLocalFile = System.IO.Path.GetTempFileName()
  102.             XceedFTP.ReceiveFile(sRemoteFile, sLocalFile)
  103.  
  104.             sData = System.IO.File.ReadAllText(sLocalFile)
  105.  
  106.             Return True
  107.         End Function
  108.         Private Sub CallbackDownload(ByVal e As IAsyncResult)
  109.             Dim t As DelegateDownload = CType(CType(e, Runtime.Remoting.Messaging.AsyncResult).AsyncDelegate, DelegateDownload)
  110.             t.EndInvoke(e)
  111.             RaiseEvent DownloadComplete(sLocalFile, sRemoteFile, sData, sDownloadProgress)
  112.         End Sub
  113.         Public Sub Download(ByVal RemoteFileName As String)
  114.             sRemoteFile = RemoteFileName
  115.             Dim t As DelegateDownload = AddressOf DoDownload
  116.             t.BeginInvoke(New AsyncCallback(AddressOf CallbackDownload), Nothing)
  117.         End Sub
  118.         Private Sub XceedFTP_FileTransferStatus(sender As Object, e As FileTransferStatusEventArgs) Handles XceedFTP.FileTransferStatus
  119.             sDownloadProgress = e
  120.         End Sub
  121.  
  122.         ' Connect Async
  123.         Private Delegate Function DelegateConnect() As Boolean
  124.         Private Function DoConnect() As Boolean
  125.             If XceedFTP.Connected = True Then
  126.                 XceedFTP.Disconnect()
  127.             End If
  128.             XceedFTP.Connect(sHost, sPort)
  129.             If XceedFTP.Connected = True Then
  130.                 XceedFTP.Login(sUsername, sPassword)
  131.                 sSeparator = XceedFTP.ServerFolderSeparator
  132.                 sCurrentPath = XceedFTP.GetCurrentFolder()
  133.  
  134.                 If sChangeFolder <> "" And sChangeFolder <> sCurrentPath Then
  135.                     XceedFTP.ChangeCurrentFolder(sChangeFolder)
  136.                     sCurrentPath = XceedFTP.GetCurrentFolder()
  137.                 End If
  138.                 Debug.Print(sCurrentPath)
  139.  
  140.                 Dim fList As FtpItemInfoList = XceedFTP.GetFolderContents()
  141.  
  142.                 sList.Files = Nothing
  143.                 sList.Folders = Nothing
  144.                 sList.Files = New Collection
  145.                 sList.Folders = New Collection
  146.  
  147.                 Dim lItem1 As New Telerik.WinControls.UI.ListViewDataItem("..")
  148.                 lItem1.Tag = ""
  149.                 lItem1.SubItems.Add("..")
  150.                 lItem1.SubItems.Add("")
  151.                 sList.Folders.Add(lItem1)
  152.  
  153.                 For Each fItem As FtpItemInfo In fList
  154.                     If fItem.Name <> "." And fItem.Name <> ".." Then
  155.                         Dim lItem As New Telerik.WinControls.UI.ListViewDataItem(fItem.Name)
  156.  
  157.                         If fItem.Type = FtpItemType.Folder Then
  158.                             lItem.Tag = ""
  159.                             lItem.SubItems.Add(fItem.Name)
  160.                             lItem.SubItems.Add(fItem.DateTime)
  161.                             sList.Folders.Add(lItem)
  162.                         Else
  163.                             lItem.SubItems.Add(fItem.Name)
  164.                             lItem.SubItems.Add(fItem.Size)
  165.                             lItem.SubItems.Add(System.IO.Path.GetExtension(fItem.Name).ToLower())
  166.                             lItem.SubItems.Add(fItem.Type.ToString)
  167.                             lItem.SubItems.Add(fItem.DateTime)
  168.                             sList.Files.Add(lItem)
  169.                         End If
  170.                     End If
  171.                 Next
  172.                 Return True
  173.             Else
  174.                 Return False
  175.             End If
  176.         End Function
  177.         Private Sub CallbackConnect(ByVal e As IAsyncResult)
  178.             Dim t As DelegateConnect = CType(CType(e, Runtime.Remoting.Messaging.AsyncResult).AsyncDelegate, DelegateConnect)
  179.             t.EndInvoke(e)
  180.             RaiseEvent Connected(sList)
  181.         End Sub
  182.         Public Sub Connect()
  183.             Dim t As DelegateConnect = AddressOf DoConnect
  184.             t.BeginInvoke(New AsyncCallback(AddressOf CallbackConnect), Nothing)
  185.         End Sub
  186.  
  187.  
  188.         ' Disconnect Async
  189.         Private Delegate Function DelegateDisconnect() As Boolean
  190.         Private Function DoDisconnect() As Boolean
  191.             XceedFTP.Disconnect()
  192.             Return Not XceedFTP.Connected
  193.         End Function
  194.         Private Sub CallbackDisconnect(ByVal e As IAsyncResult)
  195.             Dim t As DelegateDisconnect = CType(CType(e, Runtime.Remoting.Messaging.AsyncResult).AsyncDelegate, DelegateDisconnect)
  196.             t.EndInvoke(e)
  197.             RaiseEvent Disconnected()
  198.         End Sub
  199.         Public Sub Disconnect()
  200.             Dim t As DelegateDisconnect = AddressOf DoDisconnect
  201.             t.BeginInvoke(New AsyncCallback(AddressOf CallbackDisconnect), Nothing)
  202.         End Sub
  203.  
  204.  
  205.         ' Initialize Component
  206.         Public Sub New(ByVal XceedLicense As String)
  207.             Xceed.Ftp.Licenser.LicenseKey = XceedLicense
  208.             XceedFTP = New Xceed.Ftp.FtpClient
  209.         End Sub
  210.         Public Sub New(ByVal XceedLicense As String, ByVal Hostname As String)
  211.             Xceed.Ftp.Licenser.LicenseKey = XceedLicense
  212.             XceedFTP = New Xceed.Ftp.FtpClient
  213.             sHost = Hostname
  214.         End Sub
  215.         Public Sub New(ByVal XceedLicense As String, ByVal Hostname As String, ByVal IntPort As Integer)
  216.             Xceed.Ftp.Licenser.LicenseKey = XceedLicense
  217.             XceedFTP = New Xceed.Ftp.FtpClient
  218.             sHost = Hostname
  219.             sPort = IntPort
  220.         End Sub
  221.         Public Sub New(ByVal XceedLicense As String, ByVal User As String, ByVal Pass As String)
  222.             Xceed.Ftp.Licenser.LicenseKey = XceedLicense
  223.             XceedFTP = New Xceed.Ftp.FtpClient
  224.             sUsername = User
  225.             sPassword = Pass
  226.         End Sub
  227.         Public Sub New(ByVal XceedLicense As String, ByVal Hostname As String, ByVal User As String, ByVal Pass As String)
  228.             Xceed.Ftp.Licenser.LicenseKey = XceedLicense
  229.             XceedFTP = New Xceed.Ftp.FtpClient
  230.             sHost = Hostname
  231.             sUsername = User
  232.             sPassword = Pass
  233.         End Sub
  234.         Public Sub New(ByVal XceedLicense As String, ByVal Hostname As String, ByVal IntPort As Integer, ByVal User As String, ByVal Pass As String)
  235.             Xceed.Ftp.Licenser.LicenseKey = XceedLicense
  236.             XceedFTP = New Xceed.Ftp.FtpClient
  237.             sPort = IntPort
  238.             sHost = Hostname
  239.             sUsername = User
  240.             sPassword = Pass
  241.         End Sub
  242.  
  243.     End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement