public async void StartDownload() { try { ProgressBar.Tag = this; ProgressLabel.Text = ""; ProgressLabel.Visible = true; Directory.CreateDirectory(Path.GetDirectoryName(SavePath)); FileInfo file = new FileInfo(SavePath); DialogResult overwriteFile = DialogResult.Yes; if (file.Exists) { switch (MegaDownload.OverwriteMode) { case 0: overwriteFile = DialogResult.No; break; case 1: overwriteFile = DialogResult.Yes; break; case 2: if (Node.ModificationDate > file.CreationTime) { overwriteFile = DialogResult.Yes; } else { overwriteFile = DialogResult.No; } break; case 3: overwriteFile = MessageBox.Show($"File [{file.Name}] already exists. Overwrite?", "", MessageBoxButtons.YesNo); break; } if (overwriteFile == DialogResult.Yes) { file.Delete(); } } if (overwriteFile == DialogResult.Yes) { DownloadTask = MegaClient.DownloadFileAsync(Node, SavePath, Progress, MegaDownload.cancellationTokenSource.Token); await DownloadTask; } MegaDownload.UpdateQueue(this); } catch (Exception ex) { if (DownloadTask.IsCanceled) { DownloadTask.Dispose(); } //MessageBox.Show(ex.Message); } finally { //Finished = true; //ProgressBar.Tag = null; //ProgressBar.Value = 0; //ProgressLabel.Visible = false; } }
public async void StartDownload() { try { ProgressBar.Tag = this; ProgressLabel.Text = ""; ProgressLabel.Visible = true; Directory.CreateDirectory(Path.GetDirectoryName(SavePath)); FileInfo file = new FileInfo(SavePath); DialogResult overwriteFile = DialogResult.Yes; if (file.Exists) { switch (MegaDownload.OverwriteMode) { case 0: overwriteFile = DialogResult.No; break; case 1: overwriteFile = DialogResult.Yes; break; case 2: if (Node.ModificationDate > file.CreationTime) { overwriteFile = DialogResult.Yes; } else { overwriteFile = DialogResult.No; } break; case 3: overwriteFile = MessageBox.Show($"File [{file.Name}] already exists. Overwrite?", "", MessageBoxButtons.YesNo); break; } if (overwriteFile == DialogResult.Yes) { file.Delete(); } } if (overwriteFile == DialogResult.Yes) { DownloadTask = MegaClient.DownloadFileAsync(Node, SavePath, Progress, MegaDownload.cancellationTokenSource.Token); await DownloadTask; } MegaDownload.UpdateQueue(this); } catch (Exception ex) { if (DownloadTask.IsCanceled) { DownloadTask.Dispose(); if (File.Exists(SavePath)) { File.Delete(SavePath); } ProgressBar.Value = 0; ProgressLabel.Text = ""; } else { var logFileName = $"00-DOWNLOAD-LOG-{DateTime.Now.ToString("MM-dd-yyyy")}.txt"; string log = $"\n{DateTime.Now}\nNode id: {Node.Id}\nSavePath:{SavePath}\nexception: {ex.Message}\n"; File.AppendAllText(logFileName, log); } } }