private async Task <string> DownloadZip(UpdateInformation updateInfo) { if (DownloadWebClient is null) { throw new InvalidDataException(string.Format(CultureInfo.CurrentCulture, Properties.Resources.NotSet, nameof(DownloadWebClient))); } DownloadWebClient.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore); var zipSetupFilePath = Path.GetTempFileName(); if (File.Exists(zipSetupFilePath)) { File.Delete(zipSetupFilePath); } await DownloadWebClient.DownloadFileTaskAsync(updateInfo.DownloadURL, zipSetupFilePath).ConfigureAwait(false); if (File.Exists(zipSetupFilePath) == false) { throw new FileNotFoundException($"[{zipSetupFilePath}] does not exist"); } // use https://emn178.github.io/online-tools/sha512_file_hash.html var checkSum = CalculateCheckSum(zipSetupFilePath); if (updateInfo.Checksum.ToUpperInvariant() != checkSum) { throw new FileNotFoundException($"Zip file integrity check failed, [{updateInfo.Checksum}/{checkSum}]"); } return(zipSetupFilePath); }
public Downloader(string url, string savePath, string version) { _url = url; _savePath = savePath; string saveDir = Path.GetDirectoryName(savePath); if (Directory.Exists(saveDir) == false) { Directory.CreateDirectory(saveDir); } _client = new DownloadWebClient(); _client.DownloadProgressChanged += OnDownloadProgressChanged; _client.DownloadFileCompleted += OnDownloadFileCompleted; string flag; if (url.Contains("?")) { flag = "&"; } else { flag = "?"; } url += string.Format("{0}unity_download_ver={1}", flag, version); Uri uri = new Uri(url); _client.DownloadFileAsync(uri, savePath); }
private void TryCleanupExistingDownloadWebClient() { if (this.downloadWebClient == null) { return; } try { lock (this) { if (this.downloadWebClient != null) { this.downloadWebClient.DownloadFileCompleted -= OnDownloadCompleted; this.downloadWebClient.DownloadProgressChanged -= OnDownloadProgressChanged; this.downloadWebClient.OpenReadCompleted -= OnOpenReadCompleted; this.downloadWebClient.CancelAsync(); this.downloadWebClient.Dispose(); this.downloadWebClient = null; } } } catch (Exception e) { Debug.WriteLine("Error while cleaning up web client : {0}", e.Message); } }
/// <summary> /// On Form Closed /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnClosed(object sender, FormClosedEventArgs e) { if (_webClient != null) { _webClient.Dispose(); _webClient = null; } if (DialogResult != DialogResult.OK) { // Delete the file if ((DownloadedZipFile != null) && File.Exists(DownloadedZipFile)) { try { File.Delete(DownloadedZipFile); } catch { } } if ((DownloadedSetupFile != null) && File.Exists(DownloadedSetupFile)) { try { File.Delete(DownloadedSetupFile); } catch { } } } }
void ClearClient() { _client.DownloadProgressChanged -= OnDownloadProgressChanged; _client.DownloadFileCompleted -= OnDownloadFileCompleted; _client.Dispose(); _client = null; }
private DownloadWebClient CreateWebClient() { var webClient = new DownloadWebClient(); webClient.DownloadFileCompleted += OnDownloadCompleted; webClient.DownloadProgressChanged += OnDownloadProgressChanged; webClient.OpenReadCompleted += OnOpenReadCompleted; return(webClient); }
/// <summary> /// /// </summary> /// <param name="url">下载的文件地址</param> /// <param name="savePath">文件保存的地址</param> /// <param name="isAutoDeleteWrongFile">是否自动删除未下完的文件</param> public Downloader(string url, string savePath, bool isAutoDeleteWrongFile = true) { _isAutoDeleteWrongFile = isAutoDeleteWrongFile; _savePath = savePath; _client = new DownloadWebClient(); _client.DownloadProgressChanged += OnDownloadProgressChanged; _client.DownloadFileCompleted += OnDownloadFileCompleted; Uri uri = new Uri(url); _client.DownloadFileAsync(uri, savePath); }
/// <summary> /// 销毁对象,会停止所有的下载 /// </summary> public void Dispose() { if (_client != null) { _client.DownloadProgressChanged -= OnDownloadProgressChanged; _client.DownloadFileCompleted -= OnDownloadFileCompleted; _client.CancelAsync(); _client.Dispose(); _client = null; if (false == _isDone) { SetError("Canceled"); _isDone = true; } } }
/// <summary> /// 初始化下载类 /// </summary> /// <param name="url">下载文件的URL地址</param> /// <param name="savePath">保存文件的本地地址</param> /// <param name="version">URL对应文件的版本号</param> public Downloader(string url, string savePath, string version = null) { _url = url; _savePath = savePath; string saveDir = Path.GetDirectoryName(savePath); if (Directory.Exists(saveDir) == false) { Directory.CreateDirectory(saveDir); } _client = new DownloadWebClient(); _client.DownloadProgressChanged += OnDownloadProgressChanged; _client.DownloadFileCompleted += OnDownloadFileCompleted; if (null != version) { string flag; if (url.Contains("?")) { flag = "&"; } else { flag = "?"; } url += string.Format("{0}unity_download_ver={1}", flag, version); } try { Uri uri = new Uri(url); var serverPoint = ServicePointManager.FindServicePoint(uri); serverPoint.ConnectionLimit = downloadConnectionLimit; _progress = 0; _lastProgressChangedDT = DateTime.Now; _client.DownloadFileAsync(uri, savePath); } catch (Exception ex) { _isDone = true; _error = ex.Message; } }
public void InstallUpdates() { try { string downloadFile = Path.Combine(this.downloadUrl, $"{this.msiFile}{this.msiExstention}"); string saveVersionFile = Path.Combine(Paths.KnownFolder(KnownFolders.KnownFolder.Downloads), $"{this.msiFile}.{VersionManager.ServerVersion}{this.msiExstention}"); using (DownloadWebClient client = new DownloadWebClient()) { client.DownloadFile(downloadFile, saveVersionFile); } Process.Start(saveVersionFile); } catch (Exception err) { throw; } }
private void TriggerWebClientDownloadFileAsync() { Debug.WriteLine("Falling back to legacy DownloadFileAsync."); try { this.isFallback = true; var destinationDirectory = Path.GetDirectoryName(this.localFileName); if (destinationDirectory != null && !Directory.Exists(destinationDirectory)) { Directory.CreateDirectory(destinationDirectory); } TryCleanupExistingDownloadWebClient(); this.downloadWebClient = CreateWebClient(); this.downloadWebClient.DownloadFileAsync(this.fileSource, this.localFileName); Debug.WriteLine("Download async started. Source: {0} Destination: {1}", this.fileSource, this.localFileName); } catch (Exception ex) { Debug.WriteLine("Failed to download Source:{0}, Destination:{1}, Error:{2}.", this.fileSource, this.localFileName, ex.Message); } }
public bool HaveUpdates(string thisVersion) { try { CheckForUpdatesFailed = false; string downloadFile = Path.Combine(this.downloadUrl, this.versionFile); string saveVersionFile = Path.Combine(Paths.KnownFolder(KnownFolders.KnownFolder.Downloads), this.versionFile); using (DownloadWebClient client = new DownloadWebClient()) { client.DownloadFile(downloadFile, saveVersionFile); } VersionManager.ServerVersion = File.ReadAllText(saveVersionFile) .Replace("\n", string.Empty) .Replace("\r", string.Empty); File.Delete(saveVersionFile); if (!this.IsVersionNumber(VersionManager.ServerVersion)) { CheckForUpdatesFailed = true; VersionManager.ServerVersion = string.Empty; // We now need to notify the user that the system updates failed. return(true); } return(thisVersion != VersionManager.ServerVersion); } catch (Exception err) { return(false); } }
public void Start() { client = new DownloadWebClient(_cookies); ThreadPool.QueueUserWorkItem(new WaitCallback(Run)); }
/// <summary> /// Overridden to localize and start the download process /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnLoad(object sender, EventArgs e) { // Localize Text = Resources.Title_DownloadUpdateForm; messageLabel.Text = string.Format(CultureInfo.CurrentCulture, Resources.Text_DownloadProgressMessageFormat_Version, _versionInfo.Version); cancelButton.Text = Resources.Label_Cancel; // Initialize this dialog result Downloaded = false; // Temp file for Zip DownloadedZipFile = Path.GetTempFileName(); if (File.Exists(DownloadedZipFile)) { File.Delete(DownloadedZipFile); } DownloadedZipFile += ".zip"; if (File.Exists(DownloadedZipFile)) { File.Delete(DownloadedZipFile); } // Initiate download, create a temp file DownloadedSetupFile = Path.Combine(Path.GetDirectoryName(DownloadedZipFile), "DC.Crayon.Wlw.Setup.msi"); if (File.Exists(DownloadedSetupFile)) { File.Delete(DownloadedSetupFile); } // Start download to file _webClient = new DownloadWebClient(Utils.CookieContainer); _webClient.UserAgent = Utils.UserAgent; _webClient.Accept = _versionInfo.DownloadContentType; progressBar.Maximum = 1000000; _webClient.DownloadProgressChanged += this.OnDownloadProgress; _webClient.DownloadFileCompleted += this.OnDownloadCompleted; _webClient.DownloadFileAsync(new Uri(_versionInfo.DownloadUrl, UriKind.Absolute), DownloadedZipFile); }