The content of a folder on an FTP server (or of any other type of folder supported by the Xceed FileSystem) can be listed using the GetItems, GetFiles, or GetFolders methods. Using filters, the items that are listed can be limited by various criteria.
The following example demonstrates how to retrieve a listing of all ".txt" files located in an FTP server's current working directory using the steps listed below:
Create an instance of the FtpConnection class to establish a connection between the client and the FTP server. If you are using FtpConnection in a UI application, assign your form (or any other control that implements the ISynchronizeInvoke interface) to the SynchronizingObject property and call Application.DoEvents in an event; see the Events section below for an example of this.
Create an instance of an FtpFolder which will represent the folder on the FTP server whose content to list. If a folder name is not specified, the folder will represent the current working folder.
Call the FtpFolder's GetFiles method to retrieve a listing of all the ".txt" files contained in the FTP folder and loop through the collection to print the list.
Dispose of the FtpConnection once the operation is completed by calling its Dispose method or, in C#, by creating the FtpConnection instance in using blocks. If an instance of an FtpConnection object is not disposed of, connections with the FTP server may remain active until the FTP server times-out or the garbage-collector passes.
When dealing with case sensitive sources or destinations such as FTP servers, it is up to the user to pass a valid string to the method being called. For more information in case sensitivity, jump to the Case sensitivity topic.
VB.NET | Copy Code |
---|---|
|
C# | Copy Code |
---|---|
using Xceed.FileSystem; |
All methods exposed by the Xceed FileSystem's FileSystemItem, AbstractFolder, AbstractFile, and derived classes have an overload that can be used when events are required.
If you are using FtpConnection in a UI application, assign your form (or any other control that implements the ISynchronizeInvoke interface) to the SynchronizingObject property and call Application.DoEvents in an event.
With the exception of the FtpConnection's ParsingListingLine event, events can be handled by creating an instance of the FileSystemEvents class and subscribing to the desired events. For example:
VB.NET | Copy Code |
---|---|
|
C# | Copy Code |
---|---|
|
Do you want to retrieve a listing that contains all items? Use the GetItems method instead of the GetFiles method.
Do you want to retrieve a listing that contains only folders? Use the GetFolders method instead of the GetItems method.
Do you want the FTP server to initiate the data connection rather than the client-side? Set the FtpConnection's PassiveTransfer property to false.
Do you want to decrease or increase the period of time after which an FTP operation should timeout? Change the value of the FtpConnection's Timeout property.
Do you want to create a log file of the FTP process? Set the FtpConnection's TraceWriter property.
Do you want to filter (specify specific files and folders) the items that are to be listed? Use filters.
Do you want to display progress information? Create an instance of the FileSystemEvents class and handle the ByteProgression and/or ItemProgression events.
Do you want to display progress information when a folder is being scanned? Create an instance of the FileSystemEvents class and handle the ScanningFolder event.
Do you want to intervene when an error occurs with one or more of the items being manipulated? Create an instance of the FileSystemEvents class and handle the ItemException event.
Do you want to prevent routers from prematurely closing the command channel while a long data transfer is taking place. Set the KeepAliveInterval property.
Do you want quick and easy access to FTP functionalities in the same style as the ActiveX version of the Xceed FTP Library? Use the FtpClient class instead. (Note: The AsyncFtpClient should now be considered obsolete. Instead, use the FtpClient class, assigning a value to its SynchronizingObject property.)