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); }
/// <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> /// <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; } }
/// <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); }