1
votes

I'm trying to create a list all of the file names found in a folder on an FTP server. The full directory is saved in a settings database table along with the login credentials. It is connecting to the FTP area fine, and I can upload/download files to and from it.

This is my code to get the files from the folder.

lstFiles = new List<string>();

string remoteFTPPath = ftpLocation;
var request = (FtpWebRequest)WebRequest.Create(remoteFTPPath);

request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
request.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
request.Proxy = null;

FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);

List<string> directories = new List<string>();

string line = reader.ReadLine();

while (!string.IsNullOrEmpty(line))
{
   directories.Add(line);
   line = reader.ReadLine();
}
reader.Close();

The issue that I'm having is that currently the folder I'm trying to search is empty, yet it's finding 2 files.

Why is this?

The value in remoteFTPPath is ftp://ftp.myArea.co.uk/myServer.co.uk/System-Files/

This is the folder when I open it in FileZilla - it's definitely an empty folder.

enter image description here

How do I just view all of the files in System-Files and get these into a list?

FileZilla log:

Status: Logged in
Status: Retrieving directory listing of "/myArea.co.uk"...
Status: Directory listing of "/myArea.co.uk" successful
Status: Retrieving directory listing of "/myArea.co.uk/System-Files"...
Status: Directory listing of "/myArea.co.uk/System-Files" successful

And when testing the same code on a C# application, the Network.log file outputs as below.

System.Net Information: 0 : [6132] FtpWebRequest#60068066::.ctor(ftp://ftp.myServer.co.uk/myArea.co.uk/System-Files/)
System.Net Information: 0 : [6132] Current OS installation type is 'Client'.
System.Net Information: 0 : [6132] FtpWebRequest#60068066::GetResponse(Method=LIST.)
System.Net Information: 0 : [6132] FtpControlStream#34640832 - Created connection from xxx.xxx.x.xx:YYYY to xxx.xxx.xxx.x:YY.
System.Net Information: 0 : [6132] Associating FtpWebRequest#60068066 with FtpControlStream#34640832
System.Net Information: 0 : [6132] FtpControlStream#34640832 - Received response [220-Matrix FTP server ready.
220-This is a private system - No anonymous login
220-IPv6 connections are also welcome on this server.
220 Please note: files for your website must be stored under the htdocs directory.]
System.Net Information: 0 : [6132] FtpControlStream#34640832 - Sending command [USER myUser]
System.Net Information: 0 : [6132] FtpControlStream#34640832 - Received response [331 User myUser OK. Password required]
System.Net Information: 0 : [6132] FtpControlStream#34640832 - Sending command [PASS ********]
System.Net Information: 0 : [6132] FtpControlStream#34640832 - Received response [230 OK. Current directory is /]
System.Net Information: 0 : [6132] FtpControlStream#34640832 - Sending command [OPTS utf8 on]
System.Net Information: 0 : [6132] FtpControlStream#34640832 - Received response [500 Unknown command]
System.Net Information: 0 : [6132] FtpControlStream#34640832 - Sending command [PWD]
System.Net Information: 0 : [6132] FtpControlStream#34640832 - Received response [257 "/" is your current location]
System.Net Information: 0 : [6132] FtpControlStream#34640832 - Sending command [CWD /myArea.co.uk/System-Files]
System.Net Information: 0 : [6132] FtpControlStream#34640832 - Received response [250 OK. Current directory is /myArea.co.uk/System-Files]
System.Net Information: 0 : [6132] FtpControlStream#34640832 - Sending command [TYPE I]
System.Net Information: 0 : [6132] FtpControlStream#34640832 - Received response [200 TYPE is now 8-bit binary]
System.Net Information: 0 : [6132] FtpControlStream#34640832 - Sending command [PASV]
System.Net Information: 0 : [6132] FtpControlStream#34640832 - Received response [227 Entering Passive Mode (213,171,193,5,117,157)]
System.Net Information: 0 : [6132] FtpControlStream#34640832 - Sending command [LIST]
System.Net Information: 0 : [6132] FtpControlStream#34640832 - Received response [150 Accepted data connection]
System.Net Information: 0 : [6132] FtpControlStream#34640832 - Received response [226-ASCII
226-Options: -a -l
226 2 matches total]
System.Net Information: 0 : [6132] FtpWebRequest#60068066::(Releasing FTP connection#34640832.)
System.Net Information: 0 : [14644] ServicePoint#33675143 - Closed as idle.

1
In FileZilla if you check the option Server -> Force Showing Hidden Files, do you see anything else?steve16351
@steve16351 Nope, nothing in there. I'm thinking they might be system/OS files or something that can't be viewed.David
I believe what you're finding is . (current directory) and .. (parent directory.)Duston

1 Answers

1
votes

Many FTP servers return . and .. entries in a directory listing, similarly to *nix ls -a or Windows dir commands. Those are references to the directory itself and the parent directory, respectively.

You will get those for any directory, even an empty one (sometimes [e.g. on Windows servers] with an exception of the root folder).

If you do not want those, you need to filter them out in your code. What FileZilla does too for sure.