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/
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?
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.
Answers
I may be wrong... But your code looks like it is getting
Directories
and you're saying there are twoFiles
. 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.
Use the search options in the call.
https://docs.microsoft.com/en-us/dotnet/api/system.io.directory.getfiles?view=netframework-4.7.2