public void Download(string fileName, string fileUrl) { Taskes.Add(new DownloadTask() { FileName = fileName, Url = fileUrl }); }
public void Download(DownloadTask downloadTask) { Taskes.Add(downloadTask); }
public void Cancel(DownloadTask downloadTask) { Taskes.Remove(downloadTask); }
private bool StartDownload() { if (currentDownloadTask.Url == null) { throw new ArgumentNullException("url"); } currentDownloadTask.State = DownloadState.Downloading; bool isDownloadSuccessfully = false; Stream stream = null; FileStream fileStream = null; string tempFileName = Path.GetTempFileName(); downloadStopWatch.Start(); try { Uri installerUrl = new Uri(currentDownloadTask.Url); currentDownloadTask.CurrentDownloadTotalBytes = 0; currentDownloadTask.CurrentDownloadBytesSinceStopWatch = 0; using (WebClient myHttpWebClient = new WebClient()) { System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; fileStream = new FileStream(tempFileName, FileMode.Create); stream = myHttpWebClient.OpenRead(installerUrl); double contentLength = GetContentLength(myHttpWebClient); byte[] buffer = new byte[(int)contentLength]; long downloadedLength = 0; long currentTimeSpanDataLength = 0; int currentDataLength; while ((currentDataLength = stream.Read(buffer, 0, (int)contentLength)) > 0 /* && !this._cancelDownload*/) { fileStream.Write(buffer, 0, currentDataLength); downloadedLength += (long)currentDataLength; currentTimeSpanDataLength += (long)currentDataLength; if (downloadStopWatch.ElapsedMilliseconds > 800) { double num5 = (double)currentTimeSpanDataLength / 1024.0; double num6 = (double)downloadStopWatch.ElapsedMilliseconds / 1000.0; double doubleDownloadSpeed = num5 / num6; currentDownloadTask.Speed = (int)Math.Round(doubleDownloadSpeed, 0); downloadStopWatch.Reset(); downloadStopWatch.Start(); currentTimeSpanDataLength = 0; } double doubleDownloadPersent = 0.0; if (contentLength > 0.0) { doubleDownloadPersent = (double)downloadedLength / contentLength; if (doubleDownloadPersent > 1.0) { doubleDownloadPersent = 1.0; } } int intDownloadPersent = (int)(doubleDownloadPersent * 100); if (currentDownloadTask.Speed != 0) { currentDownloadTask.Percent = intDownloadPersent; //DownloadingStatusChanged(info); } } if (currentDataLength >= 0) { // download correct isDownloadSuccessfully = true; } } } catch (Exception ex2) { currentDownloadTask.DownloadFailed(ex2.Message); Taskes.Remove(currentDownloadTask); currentDownloadTask.State = DownloadState.Stop; } finally { downloadStopWatch.Stop(); if (fileStream != null) { fileStream.Close(); } if (stream != null) { stream.Close(); } } if (isDownloadSuccessfully) { if (File.Exists(currentDownloadTask.FileSavePath)) { File.Delete(currentDownloadTask.FileSavePath); } currentDownloadTask.Percent = 100; currentDownloadTask.Speed = 0; Taskes.Remove(currentDownloadTask); File.Move(tempFileName, currentDownloadTask.FileSavePath); currentDownloadTask.State = DownloadState.DownloadFinished; currentDownloadTask.DownloadFinish(); } return(isDownloadSuccessfully); }