I managed to reproduce your error on another site I have access to. After some playing around, here's my conclusion:-
When you make a FtpWebRequest
with a FTP URL that does NOT end with a /
, such as:
ftp://ftp.someftp.com/somefolder/invalidfolder
AND you specified WebRequestMethods.Ftp.ListDirectory
as the method, then what it does behind the scene is to run the following command:
NLST "somefolder/invalidfolder"
Normally, NLST will list the contents of the specified folder, and throws an exception if the folder does not exist. But because you did not specify a /
at the end of invalidfolder
, NLST will think that invalidfolder
may actually be a file (or a filename pattern). If it manages to find a folder named invalidfolder
, then and only then will it treat it as a folder. Otherwise it will try to search a file named invalidfolder
underneath the parent folder somefolder
. If the file does not exist, then one of the following will occur, depending on which FTP server software (and its configurations) is running:
- It throws an ERROR 550: File or Folder not found. (Example: spftp v1.0)
- It returns an empty result. (Example: vsFTPd v2.0.5)
In your case, the FTP server returns the latter response, and your code falls over.
The solution? Just add some validation code to make sure the ftp folder you are trying to access always has a /
at the end. Something like the following:-
if (!directory.EndsWith('/'))
directory += '/';