void WebClient_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { if (e.Cancelled) { DownloadFailureMessage?.Invoke(this, "Download was cancelled"); return; } if (e.Error != null) // We have an error! Retry a few times, then abort. { DownloadFailureMessage?.Invoke(this, "An error ocurred while trying to download file"); return; } DownloadFileCompletedWithPath?.Invoke(this, this.lastDownloadedFilePath); }
public void DownloadVideo(string videoUrl) { try { var uri = new Uri(videoUrl); using (webClient) { webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged; webClient.DownloadFileCompleted += WebClient_DownloadFileCompleted; string filePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); lastDownloadedFilePath = Path.Combine(filePath, getFilename(videoUrl)); webClient.DownloadFileAsync(uri, lastDownloadedFilePath); } } catch (Exception ex) { DownloadFailureMessage?.Invoke(this, ex.Message); } }