I have written a class that returns 2 string lists which contain the folder and file details of a ftp directory. How would I alter the class that I would be able to list all folders and files for the entire ftp directory. So basically if I supply the following ftp URL
ftp://testftp.com/Testfolder/
The class must firstly return all the folders and files for the folder "Testfolder" then it must iterate through the folders within the "TestFolder" until there are no more files and folders to iterate through. I have written the following class to get the initial root folder and files:
public List<List<string>> NewFTPListing(string user, string pass, string ftpUrl) { List<List<string>> myList = new List<List<string>>(); List<string> directInfo = new List<string>(); List<string> filesInfo = new List<string>(); try { StringBuilder result = new StringBuilder(); FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpUrl)); request.Credentials = new NetworkCredential(user, pass); //request.Method = WebRequestMethods.Ftp.ListDirectoryDetails; request.Method = WebRequestMethods.Ftp.ListDirectoryDetails; FtpWebResponse response1 = (FtpWebResponse)request.GetResponse(); StreamReader readerStream = new StreamReader(response1.GetResponseStream()); string line1 = readerStream.ReadLine(); while (!string.IsNullOrEmpty(line1)) { // MessageBox.Show(line1); result.Append(line1); result.Append("\n"); line1 = readerStream.ReadLine(); } result.Remove(result.ToString().LastIndexOf('\n'), 1); var results = result.ToString().Split('\n'); #region REGEX FIELDS //UNIX STYLE Regex regex2 = new Regex(@"^(?<month>\d{1,2})-(?<day>\d{1,2})-(?<year>\d{1,2})\s+(?<hour>\d{1,2}):(?<minutes>\d{1,2})(?<ampm>am|pm)\s+(?<dir>[<]dir[>])?\s+(?<size>\d+)?\s+(?<filename>.*)$", RegexOptions.IgnoreCase); //WIN STYLE Regex regex1 = new Regex( @"^" + //# Start of line @"(?<dir>[\-ld])" + //# File size @"(?<permission>[\-rwx]{9})" + //# Whitespace \n @"\s+" + //# Whitespace @"(?<filecode>\d+)" + @"\s+" + //# Whitespace @"(?<owner>\w+)" + @"\s+" + //# Whitespace \n @"(?<group>\w+)" + @"\s+" + //# Whitespace \n @"(?<size>\d+)" + @"\s+" + //# Whitespace \n @"(?<month>\w{3})" + //# Month (3 letters) \n @"\s+" + //# Whitespace \n @"(?<day>\d{1,2})" + //# Day (1 or 2 digits) \n @"\s+" + //# Whitespace \n @"(?<timeyear>[\d:]{4,5})" + //# Time or year \n @"\s+" + //# Whitespace \n @"(?<filename>(.*))" + //# Filename \n @"$"); //# End of line #endregion foreach (var parsed in results) { var split = regex1.Match(parsed); string fileNAAm = split.Groups["filename"].ToString();//CHECKS WHAT FTP DETAILS FORMAT IS IN: UNIX || WINDOWS if (fileNAAm.Length < 1) { //UNIX var split2 = regex2.Match(parsed); string IsDirUni = split2.Groups["dir"].ToString(); string FileName = split2.Groups["filename"].ToString();//GETTING FILENAME EXAMPLE if (IsDirUni.Equals("<DIR>", StringComparison.InvariantCultureIgnoreCase)) { if (!FileName.EndsWith(".")) { //CREATE DIRECTORY LIST string FormatVersion = "UNIX"; string Date = split2.Groups["day"].ToString() + "-" + split2.Groups["month"].ToString() + "-" + split2.Groups["year"].ToString(); string Time = split2.Groups["hour"].ToString() + ":" + split2.Groups["minutes"].ToString() + split2.Groups["ampm"].ToString(); string Size = "DIR"; string FolderName = split2.Groups["filename"].ToString(); directInfo.Add(FormatVersion + "," + Date + "," + Time + "," + Size + "," + FolderName); //lists.directInfo.Add(FormatVersion + "," + Date + "," + Time + "," + Size + "," + FolderName); // MessageBox.Show(IsDirUni + " - " + FileName); } } else { //CREATE FILE LIST string FormatVersion = "UNIX"; string Date = split2.Groups["day"].ToString() + "-" + split2.Groups["month"].ToString() + "-" + split2.Groups["year"].ToString(); string Time = split2.Groups["hour"].ToString() + ":" + split2.Groups["minutes"].ToString() + split2.Groups["ampm"].ToString(); string Size = split2.Groups["size"].ToString(); string FolderName = split2.Groups["filename"].ToString(); filesInfo.Add(FormatVersion + "," + Date + "," + Time + "," + Size + "," + FolderName); // MessageBox.Show("NOT FOLDER - " + FileName); } } else { //WINDOWS var split1 = regex1.Match(parsed); string FormatVersion = "WINDOWS"; string IsDirWin = split1.Groups["dir"].ToString(); string FileName = split1.Groups["filename"].ToString();//GETTING FILENAME EXAMPLE string size = split1.Groups["size"].ToString(); string date = split1.Groups["day"].ToString() + "-" + split1.Groups["month"].ToString(); string Time = split1.Groups["timeyear"].ToString(); if (IsDirWin.Equals("d", StringComparison.InvariantCultureIgnoreCase)) { if (!FileName.EndsWith(".")) { //CREATE DIRECTORY LIST directInfo.Add(FormatVersion + "," + date + "," + Time + "," + size + "," + FileName); //MessageBox.Show(IsDirWin + " - " + FileName); } } else { //CREATE FILE LIST filesInfo.Add(FormatVersion + "," + date + "," + Time + "," + size + "," + FileName); // MessageBox.Show("NOT FOLDER IS FILE - " + FileName); } } } myList.Add(directInfo); myList.Add(filesInfo); } catch//(Exception error) { //MessageBox.Show("Error - "+ error.message); } return myList; }