edtFTPnet/PRO - Secure FTP component for .NET | Free Trial | Pricing

How to get a directory listing

There are two basic ways to get a list of the files in a particular directory on an FTP server. The simpler way gets the names of the files using the GetFiles() method:

string[] files = ftpConnection.GetFiles();

This results in the files array containing the name of the files and directories in the current working directory on the server.

Often more information that just the name of the file/directory is required.  In such cases, the GetFileInfos() method should be used:

FTPFile[] fileDetails = ftpConnection.GetFileInfos();

This method returns an array of FTPFile objects containing information about the files in the directory - details such as the size of the file and whether or not it is a directory. 

If a listing of a directory other than the current working directory is required then the relative or absolute path may be passed to either method.  For example:

string[] files = ftpConnection.GetFiles(directoryPath);

Alternatively, the working directory may be changed as explained in How to change directories.

Note that ExFTPConnection and SecureFTPConnection also offer asynchronous versions of these methods. These are recommended for improving the responsiveness of GUI applications.  Please refer to the How to use asynchronous methods topic to learn more about this.