void InitiateDownload() { try { li = this.links.Find(c => c.Downloaded == false && c.SelForDload == true); if (li != null) { lblStatus.Content = "Getting video information . . ."; string link; yts = YouTubeService.Create(li.VideoUrl); if (li.VideoTitle == null) li.VideoTitle = yts.VideoTitle; int i = links.Count(x => x.SelForDload == true); if (i > 1) { li.LinkIndex++; string txt = "Downloading file - " + li.LinkIndex + " of " + links.Count + " - " + yts.VideoTitle; //MessageBox.Show (txt + " " + txt.Length); if (txt.Length > 86) txt = txt.Substring(0, 83) + "..."; lblStatus.Content = txt; dwnFile = OptionsWindow.AddFileNumToTitle ? saveTo + "\\" + li.LinkIndex.ToString() + ". " + yts.VideoTitle + formatText : saveTo + "\\" + yts.VideoTitle + formatText; li.LinkIndex--; } else { string txt = "Downloading file - " + yts.VideoTitle; //MessageBox.Show(txt + " " + txt.Length); if (txt.Length > 86) txt = txt.Substring(0, 83) + "..."; lblStatus.Content = txt; dwnFile = saveTo + "\\" + yts.VideoTitle + formatText; } if (OptionsWindow.Audio) { link = yts.AvailableAudioFormat[format - 1].VideoUrl; dwnFileConvAud = dwnFile; dwnFile = Path.GetTempFileName(); } else { link = yts.AvailableVideoFormat[format - 1].VideoUrl; } wc = new WebClient(); wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc_DownloadProgressChanged); wc.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(wc_DownloadFileCompleted); wc.DownloadFileAsync(new Uri(link), (dwnFile)); this.Cursor = Cursors.Arrow; } } catch (Exception exc) { Error = true; System.Windows.Forms.MessageBox.Show("A problem has ocurred, please try again", "YouTube Downloader"); LogFile.WriteToLogFile(LogFile.Operation.app_error, null, exc.Message); return; } }
void wc_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { wc = null; if (e.Cancelled) // if cancelled { li.Downloaded = false; iLink = yts; LogFile.VidSavedLoc = ""; LogFile.WriteToLogFile(LogFile.Operation.cancelled, null); MessageBox.Show("Download Cancelled", "YouTube Downloader"); try { File.Delete(dwnFile); } catch (Exception) { } links = null; yts = null; wc = null; li = null; iLink = null; WBlinkinfo.TotalCount = 0; lblData.Visibility = Visibility.Hidden; btnCancel.Visibility = Visibility.Hidden; btnOK.Visibility = Visibility.Visible; textBlock1.Visibility = Visibility.Visible; textBlock2.Visibility = Visibility.Visible; newStart = new Start(); this.NavigationService.Navigate(newStart); return; } else { if (OptionsWindow.Audio) // if audio { try { FileInfo f = new FileInfo(dwnFile); // if error if (f.Length < 4) { MessageBox.Show("Error occured during download.", "YouTube Downloader"); f = null; try { if (OptionsWindow.Audio) File.Delete(dwnFile); } catch (Exception exc) { LogFile.WriteToLogFile(LogFile.Operation.app_error, null, exc.Message); } } else { f = null; progressBar1.Value = 0; if (OptionsWindow.Audio) { toMp3(dwnFile); dwnFile = dwnFile.Substring(0, dwnFile.Length - 4) + formatText; if (File.Exists(dwnFileConvAud)) File.Delete(dwnFileConvAud); File.Copy(dwnFile, dwnFileConvAud); try { File.Delete(dwnFile); } catch (Exception exc) { LogFile.WriteToLogFile(LogFile.Operation.app_error, null, exc.Message); } } } } catch (Exception exc) { MessageBox.Show("Error: " + exc.Message, "YouTubeDownloader"); LogFile.WriteToLogFile(LogFile.Operation.app_error, null, exc.Message); } } } int i = li.LinkIndex; links[i].Downloaded = true; iLink = li; LogFile.WriteToLogFile(LogFile.Operation.downloaded, iLink); if (links.Any(x => x.Downloaded == false && x.SelForDload == true)) // to download left check InitiateDownload(); else { lblStatus.Content = "Download(s) Completed"; if (OptionsWindow.ShutdownPC) System.Diagnostics.Process.Start("shutdown", "/s /f /t 060 /c " + (char)34 + "YOUR COMPUTER WILL BE TURNED OFF IN 60 seconds" + (char)34); links = null; yts = null; wc = null; li = null; iLink = null; WBlinkinfo.TotalCount = 0; lblData.Visibility = Visibility.Hidden; btnCancel.Visibility = Visibility.Hidden; btnOK.Visibility = Visibility.Visible; textBlock1.Visibility = Visibility.Visible; textBlock2.Visibility = Visibility.Visible; } }