void OnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { DownloadFileInfo file = e.UserState as DownloadFileInfo; fileReciveCount += e.BytesReceived; FileProgress.Draw(file.FileName, fileReciveCount, file.Size); }
void OnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e) { DownloadFileInfo file = e.UserState as DownloadFileInfo; //nDownloadedTotal += file.Size; mCurrentCount++; TotalProgress.Draw(string.Format("下载总进度 {0}/{1}", mCurrentCount, mUpateCount), mCurrentCount, mUpateCount); //Debug.WriteLine(String.Format("Finish Download:{0}", file.FileName)); //替换现有文件 string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file.FileFullName); if (File.Exists(filePath)) { if (File.Exists(filePath + ".old")) { File.Delete(filePath + ".old"); } File.Move(filePath, filePath + ".old"); } File.Move(filePath + ".tmp", filePath); //继续下载其它文件 evtPerDonwload.Set(); }
private void ProcDownload() { evtPerDonwload = new ManualResetEvent(false); mCurrentCount = 0; mUpateCount = this.downloadFileList.Count; TotalProgress.Draw(string.Format("更新文件总进度 {0}/{1}", mCurrentCount, mUpateCount), mCurrentCount, mUpateCount); while (!evtDownload.WaitOne(0, false)) { if (this.downloadFileList.Count == 0) { break; } DownloadFileInfo file = this.downloadFileList[0]; fileReciveCount = 0; FileProgress.Draw(file.FileName, fileReciveCount, file.Size); //Debug.WriteLine(String.Format("Start Download:{0}", file.FileName)); //下载 clientDownload = new WebClient(); clientDownload.DownloadProgressChanged += new DownloadProgressChangedEventHandler(OnDownloadProgressChanged); clientDownload.DownloadFileCompleted += new AsyncCompletedEventHandler(OnDownloadFileCompleted); evtPerDonwload.Reset(); string tmpfilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file.FileFullName + ".tmp"); FileInfo fileInfo = new FileInfo(tmpfilePath); if (!fileInfo.Directory.Exists) { fileInfo.Directory.Create(); } clientDownload.DownloadFileAsync(new Uri(file.DownloadUrl), tmpfilePath, file); //等待下载完成 evtPerDonwload.WaitOne(); clientDownload.Dispose(); clientDownload = null; //移除已下载的文件 this.downloadFileList.Remove(file); } //Debug.WriteLine("All Downloaded"); if (this.downloadFileList.Count == 0) { Exit(true); } else { Exit(false); } evtDownload.Set(); }