/****************************** * 说明:下载进度窗口 * 创建人:龚宇超 * 创建日期: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; using Lskj.Control; using Lskj.AutoUpdate; using System.Xml; using Lskj.Util; using Lskj.Business; using System.Text.RegularExpressions; using System.Diagnostics; using Lskj.Core; using Lskj.Business.Impl; namespace Lskj.Control { /// /// 下载进度窗口 /// public partial class FrmDownload : Form { /// /// 是否下载完成 /// private bool isFinished = false; /// /// 下载总数 /// private long total = 0; /// /// 当前下载的总数 /// private long nDownloadedTotal = 0; /// /// 右键多条下载是否显示 /// public bool RightDisplay = true; private ManualResetEvent evtDownload = null; private ManualResetEvent evtPerDonwload = null; private WebClient clientDownload = null; private AutoUpdater autoUpdater = null; private delegate void SetProcessBarCallBack(int current); private delegate void ExitCallBack(bool success); private delegate void ShowCurrentDownloadFileNameCallBack(string name); public bool isBatchPrind = false; string url = null;//需要下载的url地址 string FileName = null;//需要现在的文件名称 string localFilePath = null;//需要现在的文件名称 bool isOpen = false;//是否直接打开程序 bool isprompt = true;//是否提示 public FrmDownload(string URL, string FileName, string LocalFilePath, bool isOpen = false,bool isprompt = true) { InitializeComponent(); this.url = URL; this.FileName = FileName; this.localFilePath = LocalFilePath; this.isOpen = isOpen; this.isprompt = isprompt; } /// /// 说明:窗口加载 /// 创建人:龚宇超 /// 创建日期:2018-02-23 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. private void OnFormLoad(object sender, EventArgs e) { try { if (!isBatchPrind&& RightDisplay) this.Opacity = 100D; evtDownload = new ManualResetEvent(true); evtDownload.Reset(); ThreadPool.QueueUserWorkItem(new WaitCallback(this.ProcDownload)); } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message, ex.Message); } Lskj.Control.Model.AutoSizeChange.ControllInitializeSize(this); } /// /// 说明: /// 创建人:龚宇超 /// 创建日期:2018-02-23 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The sender. /// The instance containing the event data. private void OnFormClosing(object sender, FormClosingEventArgs e) { try { 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(); } } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message, ex.Message); } } /// /// 说明:下载文件 /// 创建人:龚宇超 /// 创建日期:2018-02-23 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The o. private void ProcDownload(object o) { DownloadFileInfo file = new DownloadFileInfo(url, FileName, "", 0, ""); evtPerDonwload = new ManualResetEvent(false); try { //Debug.WriteLine(String.Format("Start Download:{0}", file.FileName)); this.ShowCurrentDownloadFileName(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(); this.SetProcessBar(e.ProgressPercentage); } 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; this.SetProcessBar(100); evtPerDonwload.Set(); } catch (Exception exp) { MessageBox.Show(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); clientDownload.DownloadFileAsync(new Uri(file.DownloadUrl), localFilePath, file); //Wait for the download complete evtPerDonwload.WaitOne(); isFinished = true; } catch (Exception e) { string Message = ErrorMessage.PromptErrorMessage(e); MessageUtil.Show(Message, e.Message); } //Test network and deal with errors if there have //DealWithDownloadErrors(); if (isFinished) { if (!isBatchPrind&& RightDisplay&& this.isprompt) MessageUtil.Show("附件下载成功!"); OpenDownFile(); Exit(true); } else { Exit(false); } evtDownload.Set(); } /// /// 说明:直接打开下载完成的文件 /// 创建人:王一帆 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The URL. /// Name of the file. private void OpenDownFile() { if (!isOpen) return; string v_OpenFilePath = localFilePath; //存在则打开 System.Diagnostics.Process.Start(v_OpenFilePath); } /// /// 关闭触发事件删除webclient下载资源 /// /// /// private void FrmDownload_FormClosing(object sender, FormClosingEventArgs e) { try { clientDownload.Dispose(); clientDownload = null; } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show(Message, ex.Message); } } /// /// 说明:显示当前下载文件 /// 创建人:龚宇超 /// 创建日期: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) { if (this.progressBarTotal.InvokeRequired) { SetProcessBarCallBack cb = new SetProcessBarCallBack(SetProcessBar); this.Invoke(cb, new object[] { current }); } else { this.progressBarTotal.Value = current; } } /// /// 说明:退出下载程序 /// 创建人:王一帆 /// 创建日期:2020-04-02 /// 修改人: /// 修改日期: /// 修改备注: /// 版本: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) { try { if (!isFinished && DialogResult.Yes == MessageBox.Show("附件正在下载确定取消下载吗?", ConstModel.LS_TITLE_NAME, MessageBoxButtons.YesNo, MessageBoxIcon.Question)) { evtDownload.Set(); evtPerDonwload.Set(); Exit(false); } } catch (Exception ex) { string Message = ErrorMessage.PromptErrorMessage(ex); MessageUtil.Show("附件下载取消出错!" + Message); } } } }