Use FTP to get a list of files from multiple sub folders

  • Thread starter Thread starter Bogdan-Sorin
  • Start date Start date
B

Bogdan-Sorin

Guest
I'm trying to get a list of all files from a root folder using FTP. That folder contains multiple sub folders and so on.

Ive been looking for a some hours now and I'm not sure if is even possible.

This is what I tried but only returns only the subfolders that are in the root folder:


private List<string> GetAllFtpFiles(string folderPath)
{
try
{
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(folderPath);
ftpRequest.Credentials = new NetworkCredential("username", "password");
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());

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

string line = streamReader.ReadToEnd();
while (string.IsNullOrEmpty(line) == false)
{
var lineArr = line.Split('/');
line = lineArr[lineArr.Count() - 1];
directories.Add(line);
line = streamReader.ReadLine();
}

streamReader.Close();

return directories;
}
catch (Exception ex)
{
throw ex;
}
}

Is this even possible? Can anyone point me in the right direction?

Continue reading...
 
Back
Top