/****************************** * 说明:下载进度窗口 * 创建人:龚宇超 * 创建日期:2018-02-09 * 修改人: * 修改日期: * 修改备注: * 版本:1.0.0.0 ******************************/ using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; using System.Threading; using System.Net; namespace Lskj.AutoUpdate { /// /// 下载进度窗口 /// public partial class FrmProgress : Form { /// /// 是否下载完成 /// private bool isFinished = false; /// /// 下载总数 /// private long total = 0; /// /// 当前下载的总数 /// private long nDownloadedTotal = 0; /// /// 下载文件列表 /// private List downloadFileList = new List(); /// /// 所有文件列表 /// private List allFileList = null; private ManualResetEvent evtDownload = null; private ManualResetEvent evtPerDonwload = null; private WebClient clientDownload = null; private AutoUpdater autoUpdater = null; private delegate void SetProcessBarCallBack(int current, int total); private delegate void ExitCallBack(bool success); private delegate void ShowCurrentDownloadFileNameCallBack(string name); public FrmProgress(List downloadFileListTemp, AutoUpdater apd) { InitializeComponent(); this.autoUpdater = apd; this.downloadFileList = downloadFileListTemp; this.allFileList = downloadFileListTemp.Select(x => new DownloadFileInfo(x.DownloadUrl, x.FileName, x.Version, x.Size, x.LastVersion)).ToList(); //foreach (DownloadFileInfo item in downloadFileListTemp) //{ // // 本地临时文件夹中不存在此文件则下载. // string tempUrlPath = CommonUnitity.GetFolderUrl(item); // tempUrlPath = Path.Combine(CommonUnitity.SystemBinUrl + ConstModel.TEMP_FOLDER_NAME + tempUrlPath, item.FileName); // if (!File.Exists(tempUrlPath)) // { // this.downloadFileList.Add(item); // } //} this.lblAll.Text = string.Format("总进度(0/{0})", this.downloadFileList.Count); } /// /// 说明:窗口加载 /// 创建人:龚宇超 /// 创建日期:2018-02-23 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. private void OnFormLoad(object sender, EventArgs e) { evtDownload = new ManualResetEvent(true); evtDownload.Reset(); ThreadPool.QueueUserWorkItem(new WaitCallback(this.ProcDownload)); } /// /// 说明: /// 创建人:龚宇超 /// 创建日期:2018-02-23 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. private void OnFormClosing(object sender, FormClosingEventArgs e) { if (!isFinished && DialogResult.Yes == MessageBox.Show(ConstModel.UPDATE_GIVING_UP, ConstModel.LS_TITLE_NAME, MessageBoxButtons.YesNo, MessageBoxIcon.Question)) { e.Cancel = true; return; } else { if (clientDownload != null) clientDownload.CancelAsync(); evtDownload.Set(); evtPerDonwload.Set(); } } /// /// 说明:下载文件 /// 创建人:龚宇超 /// 创建日期:2018-02-23 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The o. private void ProcDownload(object o) { string tempFolderPath = Path.Combine(CommonUnitity.SystemBinUrl, ConstModel.TEMP_FOLDER_NAME); if (!Directory.Exists(tempFolderPath)) { Directory.CreateDirectory(tempFolderPath); } evtPerDonwload = new ManualResetEvent(false); foreach (DownloadFileInfo file in this.downloadFileList) { total += file.Size; } int count = this.downloadFileList.Count; while (!evtDownload.WaitOne(0, false)) { try { if (this.downloadFileList.Count == 0) break; this.lblAll.Text = string.Format("总进度({0}/{1})", count - this.downloadFileList.Count, count); DownloadFileInfo file = this.downloadFileList[0]; //Debug.WriteLine(String.Format("Start Download:{0}", file.FileName)); this.ShowCurrentDownloadFileName(file.FileName); //Download clientDownload = new WebClient(); //Added the function to support proxy //clientDownload.Proxy = System.Net.WebProxy.GetDefaultProxy(); clientDownload.Proxy = WebRequest.GetSystemWebProxy(); clientDownload.Proxy.Credentials = CredentialCache.DefaultCredentials; clientDownload.Credentials = System.Net.CredentialCache.DefaultCredentials; //End added clientDownload.DownloadProgressChanged += (object sender, DownloadProgressChangedEventArgs e) => { try { //this.Text = "接收:" + e.BytesReceived.ToString() + " 字节数:" + progressBarCurrent.Maximum.ToString(); progressBarCurrent.Value = (int)e.BytesReceived / progressBarCurrent.Maximum; progressBarTotal.Value = (int)((nDownloadedTotal + e.BytesReceived) * 100 / total); this.SetProcessBar(e.ProgressPercentage, (int)((nDownloadedTotal + e.BytesReceived) * 100 / total)); } catch (Exception ex) { //MessageBox.Show(ex.Message); //log the error message,you can use the application's log code } }; clientDownload.DownloadFileCompleted += (object sender, AsyncCompletedEventArgs e) => { try { DealWithDownloadErrors(); DownloadFileInfo dfile = e.UserState as DownloadFileInfo; nDownloadedTotal += dfile.Size; this.SetProcessBar(0, (int)(nDownloadedTotal * 100 / total)); evtPerDonwload.Set(); } catch (Exception exp) { MessageBox.Show("下载错误202:" + exp.Message); //log the error message,you can use the application's log code } }; evtPerDonwload.Reset(); //Download the folder file string tempFolderPath1 = CommonUnitity.GetFolderUrl(file); if (!string.IsNullOrEmpty(tempFolderPath1)) { tempFolderPath = Path.Combine(CommonUnitity.SystemBinUrl, ConstModel.TEMP_FOLDER_NAME); tempFolderPath += tempFolderPath1; } else { tempFolderPath = Path.Combine(CommonUnitity.SystemBinUrl, ConstModel.TEMP_FOLDER_NAME); } clientDownload.DownloadFileAsync(new Uri(file.DownloadUrl), Path.Combine(tempFolderPath, file.FileName), file); //Wait for the download complete evtPerDonwload.WaitOne(); clientDownload.Dispose(); clientDownload = null; //Remove the downloaded files this.downloadFileList.Remove(file); } catch (Exception e) { MessageBox.Show(this.downloadFileList[0].FileName + "\r\n" + e.Message); ShowErrorAndRestartApplication(); //throw; } } //When the files have not downloaded,return. if (downloadFileList.Count > 0) { return; } //Test network and deal with errors if there have DealWithDownloadErrors(); //Debug.WriteLine("All Downloaded"); foreach (DownloadFileInfo file in this.allFileList) { string tempUrlPath = CommonUnitity.GetFolderUrl(file); string oldPath = string.Empty; string newPath = string.Empty; try { if (!string.IsNullOrEmpty(tempUrlPath)) { oldPath = Path.Combine(CommonUnitity.SystemBinUrl + tempUrlPath.Substring(1), file.FileName); newPath = Path.Combine(CommonUnitity.SystemBinUrl + ConstModel.TEMP_FOLDER_NAME + tempUrlPath, file.FileName); } else { oldPath = Path.Combine(CommonUnitity.SystemBinUrl, file.FileName); newPath = Path.Combine(CommonUnitity.SystemBinUrl + ConstModel.TEMP_FOLDER_NAME, file.FileName); } //just deal with the problem which the files EndsWith xml can not download System.IO.FileInfo f = new FileInfo(newPath); if (!file.Size.ToString().Equals(f.Length.ToString()) && !file.FileName.ToString().EndsWith(".xml")) { MessageBox.Show($"文件大小与服务器不匹配"); ShowErrorAndRestartApplication(); } //Added for dealing with the config file download errors string newfilepath = string.Empty; if (newPath.Substring(newPath.LastIndexOf(".") + 1).Equals(ConstModel.CONFIG_FILE_KEY)) { if (System.IO.File.Exists(newPath)) { if (newPath.EndsWith("_")) { newfilepath = newPath; newPath = newPath.Substring(0, newPath.Length - 1); oldPath = oldPath.Substring(0, oldPath.Length - 1); } File.Move(newfilepath, newPath); } } //End added if (File.Exists(oldPath)) { MoveFolderToOld(oldPath, newPath); } else { //Edit for config_ file if (!string.IsNullOrEmpty(tempUrlPath)) { if (!Directory.Exists(CommonUnitity.SystemBinUrl + tempUrlPath.Substring(1))) { Directory.CreateDirectory(CommonUnitity.SystemBinUrl + tempUrlPath.Substring(1)); MoveFolderToOld(oldPath, newPath); } else { MoveFolderToOld(oldPath, newPath); } } else { MoveFolderToOld(oldPath, newPath); } } } catch (Exception exp) { MessageBox.Show("oldPath:" + oldPath + "\r\n" + "newPath:" + newPath + "\r\n" + exp.Message); //log the error message,you can use the application's log code } } //After dealed with all files, clear the data this.allFileList.Clear(); if (this.downloadFileList.Count == 0) Exit(true); else Exit(false); evtDownload.Set(); } /// /// 说明:移动文件 /// 创建人:龚宇超 /// 创建日期:2018-02-23 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The old path. /// The new path. private void MoveFolderToOld(string oldPath, string newPath) { if (File.Exists(oldPath + ".old")) System.IO.File.SetAttributes(oldPath + ".old", System.IO.FileAttributes.Normal); if (File.Exists(oldPath)) System.IO.File.SetAttributes(oldPath, System.IO.FileAttributes.Normal); if (File.Exists(oldPath + ".old")) File.Delete(oldPath + ".old"); if (File.Exists(oldPath)) File.Move(oldPath, oldPath + ".old"); File.Move(newPath, oldPath); } /// /// 说明:显示当前下载文件 /// 创建人:龚宇超 /// 创建日期:2018-02-23 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The name. private void ShowCurrentDownloadFileName(string name) { if (this.labelCurrentItem.InvokeRequired) { ShowCurrentDownloadFileNameCallBack cb = new ShowCurrentDownloadFileNameCallBack(ShowCurrentDownloadFileName); this.Invoke(cb, new object[] { name }); this.labelCurrentItem.Text = "正在更新:" + name; } else { this.labelCurrentItem.Text = "正在更新:" + name; } } /// /// 说明:显示当前进度 /// 创建人:龚宇超 /// 创建日期:2018-02-23 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The current. /// The total. private void SetProcessBar(int current, int total) { if (this.progressBarCurrent.InvokeRequired) { SetProcessBarCallBack cb = new SetProcessBarCallBack(SetProcessBar); this.Invoke(cb, new object[] { current, total }); } else { this.progressBarCurrent.Value = current; this.progressBarTotal.Value = total; } } /// /// 说明:退出升级程序 /// 创建人:龚宇超 /// 创建日期:2018-02-23 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// if set to true [success]. private void Exit(bool success) { if (this.InvokeRequired) { ExitCallBack cb = new ExitCallBack(Exit); this.Invoke(cb, new object[] { success }); } else { this.isFinished = success; this.DialogResult = success ? DialogResult.OK : DialogResult.Cancel; this.Close(); } } /// /// 说明:取消 /// 创建人:龚宇超 /// 创建日期:2018-02-23 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. private void OnCancel(object sender, EventArgs e) { if (!isFinished && DialogResult.Yes == MessageBox.Show(ConstModel.UPDATE_GIVING_UP, ConstModel.LS_TITLE_NAME, MessageBoxButtons.YesNo, MessageBoxIcon.Question)) { evtDownload.Set(); evtPerDonwload.Set(); autoUpdater.RollBack(); Exit(false); } } /// /// 说明:下载失败 /// 创建人:龚宇超 /// 创建日期:2018-02-23 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// private void DealWithDownloadErrors() { try { //Test Network is OK or not. Config config = new Config().LoadConfig(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConstModel.AUTOUPDATE_CONFIG)); WebClient client = new WebClient(); client.DownloadString(config.ServerUrl); } catch (Exception e) { //MessageBox.Show(e.Message); //log the error message,you can use the application's log code ShowErrorAndRestartApp