private async Task DownloadFtpFolderAsync(FtpFolder argFolder, String argLocalRoot) { // Make all the necessary local folders String localDestFolder = Path.Combine(argLocalRoot, argFolder.Name); if (!Directory.Exists(localDestFolder)) { Directory.CreateDirectory(localDestFolder); } List <Job> jobs = new List <gFtpGUI.Job>(); foreach (FtpFile file in argFolder.Files.OrderBy(f => f.Name)) { Job j = new gFtpGUI.Job { AriaPath = txtAria.Text, FtpPath = argFolder.FullPath, FtpFilename = file.Name, LocalPath = localDestFolder, LocalFilename = file.Name, Size = new FileSize(file.Size), Ftp = _ftp }; jobs.Add(j); Application.DoEvents(); } await _FrmQueue.AddJobsAsync(jobs); foreach (FtpFolder folder in argFolder.Folders) { if (folder.Name != "..." && folder.Name != ".." && folder.Name != ".") { await DownloadFtpFolderAsync(folder, localDestFolder); } } }
private async void btnDownload_Click(object sender, EventArgs e) { try { if (grdFtpFiles.SelectedIndex == -1) { throw new Exception("No Ftp File selected!"); } if (String.IsNullOrWhiteSpace(txtLocalPath.Text)) { throw new Exception("No local path was selected!"); } if (String.IsNullOrWhiteSpace(txtAria.Text)) { throw new Exception("No Aria2 path was provided!"); } grpFtpFiles.Enabled = false; grpFtpServer.Enabled = false; grpFtpFiles.Cursor = Cursors.WaitCursor; grpFtpServer.Cursor = Cursors.WaitCursor; Application.DoEvents(); var selectedFtpItems = grdFtpFiles.SelectedItems.Cast <FileItem>().OrderBy(f => f.Name); foreach (FileItem f in selectedFtpItems) { if (f.Type != "Directory") { Job j = new gFtpGUI.Job { AriaPath = txtAria.Text, FtpPath = txtFtpPath.Text, FtpFilename = f.Name, LocalPath = txtLocalPath.Text, LocalFilename = f.Name, Size = f.Size, Ftp = _ftp }; await _FrmQueue.AddJobAsync(j); } else { // First we have to get all the Folders and Directories are inside FtpFolder fd = await _ftp.GetFtpFolderDetailsAsync(UrlHelper.Combine(txtFtpPath.Text, f.Name), Int32.MaxValue); await DownloadFtpFolderAsync(fd, txtLocalPath.Text); } } grpFtpFiles.Enabled = true; grpFtpServer.Enabled = true; grpFtpFiles.Cursor = Cursors.Default; grpFtpServer.Cursor = Cursors.Default; } catch (Exception ex) { Debug.WriteLine(ex); grpFtpFiles.Cursor = Cursors.Default; grpFtpServer.Cursor = Cursors.Default; MessageBox.Show(ex.Message, "An error has occured!", MessageBoxButtons.OK, MessageBoxIcon.Error); grpFtpFiles.Enabled = true; grpFtpServer.Enabled = true; } }