public Progress ( int place, int total ) : void | ||
place | int | Current progress to this point |
total | int | Total progress to completion |
return | void |
/// <summary> /// Given a URL, download the file from that URL to a destination file. Returns whether the download /// was successful. This automatically adds the download to the list of disposables, and removes it /// when the download is completed. /// </summary> /// <param name="url">The URL to download the file from</param> /// <param name="dest">The relative or absolute path to the destination file to be saved</param> /// <returns>True if the download is successful</returns> private bool DownloadFile(string url, string dest) { ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; //TLS 1.2 using (WebClient client = new WebClient()) { _disposables.Add(client); try { client.DownloadProgressChanged += (sender, e) => { double bytesIn = e.BytesReceived; double totalBytes = e.TotalBytesToReceive; if (totalBytes < 0) { _form.ProgressIndeterminate(); } else { _form.Progress((int)bytesIn, (int)totalBytes); } }; client.DownloadFileAsync(new Uri(url), dest); // Make this thread wait for the download to finish while (client.IsBusy) { Thread.Sleep(50); } } catch (Exception) { return(false); } finally { _disposables.Remove(client); } } return(true); }
/// <summary> /// Given a URL, download the file from that URL to a destination file. Returns whether the download /// was successful. This automatically adds the download to the list of disposables, and removes it /// when the download is completed. /// </summary> /// <param name="url">The URL to download the file from</param> /// <param name="dest">The relative or absolute path to the destination file to be saved</param> /// <returns>True if the download is successful</returns> private bool DownloadFile(string url, string dest) { using (WebClient client = new WebClient()) { _disposables.Add(client); try { client.Headers["User-Agent"] = "BuildToolsGui"; client.DownloadProgressChanged += (sender, e) => { double bytesIn = e.BytesReceived; double totalBytes = e.TotalBytesToReceive; if (totalBytes < 0) { _form.ProgressIndeterminate(); } else { _form.Progress((int)bytesIn, (int)totalBytes); } }; client.DownloadFileAsync(new Uri(url), dest); // Make this thread wait for the download to finish while (client.IsBusy) { Thread.Sleep(50); } } catch (Exception) { return(false); } finally { _disposables.Remove(client); } } return(true); }