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 thedirectoryNameList() method:
string[] files = ftp.directoryNameList();
This
results in the files array containing the name of the files and
directories in the current working directory on the server.
FTP
servers can return more information than just the names of the files
and directories. This information can be returned as an array of
strings by using an overloaded directoryNameList()
method. The 'full' boolean flag must be set to true to get the full
listing:
For
convenience, the detailed file and directory information can be
interpreted and returned as an object, in the directoryList() method:
FTPFile[] files =
ftp.directoryList();
This
method returns an array ofFTPFile 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 it is recommended that the remote working directory be changed
to that directory, as described in How
to change directories.
The relative or absolute path may be passed to either method, but
some FTP servers do not support this feature. In these cases changing
directories should work.
Changing
locale
Sometimes
the directoryList() method will fail with a
ParseException, citing a date string that it cannot interpret. This is
often because the FTP server is in a different locale to the client.
It may be returning month names that cannot be understood in the
client's locale. For example, an FTP server in a German locale will
return German month names, which cannot be interpreted by a client in a
locale that uses English. To solve this, the server's locale should be
set in the client, before connecting, via the setParserLocales
method: