Issue listing file names from FTP directory

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))
       line = reader.ReadLine();

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

I have also opened the folder in Windows Explorer and in FileZilla - it's definitely an empty folder.

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


Best Answer


  • DavidH_1997DavidH_1997 Member ✭✭

    @ClintStLaurent said:
    I may be wrong... But your code looks like it is getting Directories and you're saying there are two Files. So that might cause some discrepancies.

    Many systems have hidden files/folders.
    Some that start with a period are hidden. Some OSes hide files that start with an underscore.
    To keep the user from hurting themselves.
    So just because Windows says its empty, doesn't mean it is.

    That's a good point, I'd not considered that - is there a way to only find files ending in a certain extension? I only need to list .zips, so it seems pointless to loop over all files in the directory.

Sign In or Register to comment.