I have made some code that connects to an FTP server.
My problem is that I against some servers get both folder and filename, e.g. myfolder\myfile.txt
, and others just get myfile.txt
.
var request = (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/myfolder");
request.Method = WebRequestMethods.Ftp.ListDirectory;
var ftpResponse = (FtpWebResponse) request.GetResponse();
var ftpResponeStream = ftpResponse.GetResponseStream();
var ftpStreamReader = new StreamReader(ftpResponeStream);
string line;
while ((line = ftpStreamReader.ReadLine()) != null)
{
Console.WriteLine(line);
}
I would prefer that I just get myfile.txt
because that is how the real code should run, but I don't know, if this is a FileZilla setting or something else.
The URL for the ListDirectory
method should end with a slash, in general.
Without a slash, results tend to be uncertain, largely depending on the FTP server implementation.
(FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/myfolder/");
With a URL like ftp://ftp.example.com/parent/folder
, without the slash, the FtpWebRequest
does:
CWD /parent
NLST folder
In this case, some FTP servers include the folder
in the listing, while some do not.
While with a slash, the FtpWebRequest
does:
CWD /parent/folder
NLST
In this case, the listing includes bare filenames.