private void StackDirectories(Stack <DirectoryInfo> p_StackDeep, DirectoryInfo p_Current) { p_StackDeep.Push(p_Current); foreach (DirectoryInfo l_Child in p_Current.GetFolders().ToArray()) { this.StackDirectories(p_StackDeep, l_Child); } return; }
public IEnumerable <Downloader> CreateFileDownloaders(string localBasePath, bool recursive = false) { if (!this.Exists) { throw new ArgumentException("The given folder doesnt exist into the ftp server"); } if (String.IsNullOrWhiteSpace(localBasePath)) { throw new ArgumentException("The given folder doesnt exist into the ftp server"); } List <Downloader> l_Downloaders = new List <Downloader>(); // Files at root foreach (FileInfo l_FtpFile in this.GetFiles().ToArray()) { l_Downloaders.Add( new DownloaderFile( fileFTP: l_FtpFile, fileHD: new System.IO.FileInfo(string.Format(@"{0}\{1}", localBasePath, l_FtpFile.Name)) ) ); } if (!recursive) { return(l_Downloaders); } // Traverse files recursively Queue <DirectoryInfo> l_QueueTraverseFolders = new Queue <DirectoryInfo>(); // Enqueu folders for the root foreach (DirectoryInfo child in this.GetFolders().ToArray()) { l_QueueTraverseFolders.Enqueue(child); } while (0 != l_QueueTraverseFolders.Count) { DirectoryInfo l_FtpDir = l_QueueTraverseFolders.Dequeue(); foreach (FileInfo l_FtpFile in l_FtpDir.GetFiles().ToArray()) { string l_AddedPath = l_FtpFile.Directory.FullPath.Remove(0, this.FullPath.Length); string l_Folder_HD = string.Format(@"{0}\{1}\", localBasePath, l_AddedPath).ToValidPath_HD(); string l_File_HD = l_Folder_HD + l_FtpFile.Name; l_Downloaders.Add( new DownloaderFile( fileFTP: l_FtpFile, fileHD: new System.IO.FileInfo(l_File_HD) ) ); } // Enqueu folders for the next level foreach (DirectoryInfo child in l_FtpDir.GetFolders().ToArray()) { l_QueueTraverseFolders.Enqueue(child); } } return(l_Downloaders); }