This example demonstrates the 8 following steps for listing files with the ListFolderContents method:
-
Specify the FTP server's address
-
Specify the username to login
-
Specify the password for the username
-
Connect to the FTP server
-
Begin the listing process
-
Process each item being listed
-
Disconnect from the FTP server
-
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 'Listbox1' 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.
Delphi |
Copy Code |
begin XceedFtp1.Licenser( 'your license key' );
XceedFtp1.ServerAddress := 'ftp.cdrom.com'; {step (1)} XceedFtp1.UserName := 'anonymous'; {step (2)} XceedFtp1.Password := 'guest'; {step (3)}
try {step (8)} XceedFtp1.Connect(); {step (4)} ShowMessage('Successfully connected to server. Starting the listing of the contents of the current folder.');
try {step (8)} XceedFtp1.ListFolderContents(''); {step (5)} except on xErr: Exception do ShowMessage('Error while listing. Description: ' + xErr.Message) end;
try {step (8)} XceedFtp1.Disconnect(); {step (7)} except on xErr: Exception do ShowMessage('Error while disconnecting. Description: ' + xErr.Message) end;
except on xErr: Exception do ShowMessage('Error while connecting. Description: ' + xErr.Message) end; end;
{ 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.}
procedure TForm1.XceedFtp1ListingFolderItem(Sender: TObject; const sName: WideString; dtDate: TDateTime; lFileSize: Integer; eItemType: TOleEnum; const sUserData: WideString); var sFileSize: String; begin sFileSize := IntToStr(lFileSize);
case eItemType of fitFile: ListBox1.Items.Add('File: ' + sName + ' (' + sFileSize + ' bytes)'); fitFolder: ListBox1.Items.Add('Folder: ' + sName); fitLink: ListBox1.Items.Add('Link: ' + sName + ' (' + sFileSize + ' bytes)'); end; end;
|