Xceed FTP Library Documentation
Listing a remote folder's contents (VB example)
Example topics > Listing a remote folder's contents (VB example)

This example demonstrates the 8 following steps for listing files with the ListFolderContents method:

  1. Specify the FTP server's address
  2. Specify the username to login
  3. Specify the password for the username
  4. Connect to the FTP server
  5. Begin the listing process
  6. Process each item being listed
  7. Disconnect from the FTP server
  8. Perform error handling

The following code assumes you have placed an XceedFtp control and a Listbox on a form and have called them 'XceedFtp1' and 'List1' respectively (the default names).

The first section of code below demonstrates all steps except step (6). Step (6), the handler for the ListingFolderItem event, is found right after.

Visual Basic Copy Code

Call XceedFtp1.License( "your license key" )

XceedFtp1.ServerAddress = "ftp.cdrom.com" ' step (1)
XceedFtp1.UserName = "anonymous" ' step (2)
XceedFtp1.Password = "guest" ' step (3)

On Error Resume Next ' step (8)

Call XceedFtp1.Connect ' step (4)

If Err.Number = 0 Then ' step (8)
   MsgBox "Successfully connected to FTP server. Starting to list the contents of the current folder."

   Call XceedFtp1.ListFolderContents("") ' step (5)

   If Err.Number <> 0 Then ' step (8)
      MsgBox "Error while listing remote folder contents. Description: '" & Err.Description & "'. Error code#" & Err.Number
   End If

   Call XceedFtp1.Disconnect ' step (7)
Else
   MsgBox "Error while connecting. Description: '" & Err.Description & "'. Error code#" & Err.Number
End If

' Step (6): The following code which adds the name and size of each
' item being listed into a listbox on your form) must go into the error
' handler for the ListingFolderItem event. Note: Do not copy and paste the
' event declaration from the code below. Have the header automatically
' generated by VB. You can copy the code between the header and the
' 'End Sub' though.

Private Sub XceedFtp1_ListingFolderItem(ByVal sName As String, ByVal dtDate As Date, ByVal lFileSize As Long, ByVal eItemType As XceedFtpLibCtl.EXFFolderItemType, ByVal sUserData As String)
   Select Case eItemType
      Case fitFile
         List1.AddItem ("File: " & sName & " (" & CStr(lFileSize)) & " bytes)"
      Case fitFolder
         List1.AddItem ("Folder: " & sName)
      Case fitLink
         List1.AddItem ("Link: " & sName & " (" & CStr(lFileSize)) & " bytes)"
   End Select
End Sub