This example demonstrates the 8 following steps for listing files with the GetFolderContents method:
-
Specify the FTP server's address
-
Specify the username to login
-
Specify the password for the username
-
Connect to the FTP server
-
Obtain the directory listing object
-
Disconnect from the FTP server
-
Process the listed items
-
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)
Delphi |
Copy Code |
var xFolderItems : XceedFtpFolderItems; {for step (5)} xItem : XceedFtpFolderItem; {for step (6)} nItemIndex : integer; {for step (6)} vaIndex : OleVariant; {for step (6)} begin XceedFtp1.License( '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. Obtaining folder contents.');
try {step (8)}
xFolderItems := XceedFtp1.GetFolderContents('',fcfCollection) As IXceedFtpFolderItems; {step (5)}
ShowMessage('Successfully obtained the contents of the current remote folder');
XceedFtp1.Disconnect(); {step (6)}
nItemIndex := 1; {step (7)}
while nItemIndex <= xFolderItems.Count do {step (7)} begin vaIndex := nItemIndex; xItem := xFolderItems.Item[ vaIndex ]; {step (7)}
ListBox1.Items.Add(xItem.ItemName + ' -> Size : ' + IntToStr(xItem.FileSize));
nItemIndex := nItemIndex + 1; end; except {step (8)} on xErr: Exception do ShowMessage('Error while getting folder contents. Description: ' + xErr.Message) end; except on xErr: Exception do ShowMessage('Error while connecting. Description: ' + xErr.Message) end; end; |