7cb7cdeaf2
SVN-Revision: r790
349 lines
12 KiB
C#
349 lines
12 KiB
C#
/******************************
|
|
* 说明:下载进度窗口
|
|
* 创建人:龚宇超
|
|
* 创建日期: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
|
|
{
|
|
/// <summary>
|
|
/// 下载进度窗口
|
|
/// </summary>
|
|
public partial class FrmDownload : Form
|
|
{
|
|
/// <summary>
|
|
/// 是否下载完成
|
|
/// </summary>
|
|
private bool isFinished = false;
|
|
/// <summary>
|
|
/// 下载总数
|
|
/// </summary>
|
|
private long total = 0;
|
|
/// <summary>
|
|
/// 当前下载的总数
|
|
/// </summary>
|
|
private long nDownloadedTotal = 0;
|
|
/// <summary>
|
|
/// 右键多条下载是否显示
|
|
/// </summary>
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// <para>说明:窗口加载</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-02-23 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="sender">The sender.</param>
|
|
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
|
|
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);
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-02-23 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="sender">The sender.</param>
|
|
/// <param name="e">The <see cref="FormClosingEventArgs"/> instance containing the event data.</param>
|
|
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);
|
|
}
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// <para>说明:下载文件</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-02-23 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="o">The o.</param>
|
|
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();
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:直接打开下载完成的文件</para>
|
|
/// <para>创建人:王一帆</para>
|
|
/// <para>创建日期: </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="URL">The URL.</param>
|
|
/// <param name="FileName">Name of the file.</param>
|
|
private void OpenDownFile()
|
|
{
|
|
try
|
|
{
|
|
if (!isOpen) return;
|
|
string v_OpenFilePath = localFilePath;
|
|
//存在则打开
|
|
System.Diagnostics.Process.Start(v_OpenFilePath);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
MessageUtil.Show($"打开附件失败\r\n{ex.Message}");
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 关闭触发事件删除webclient下载资源
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
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);
|
|
}
|
|
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:显示当前下载文件</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-02-23 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="name">The name.</param>
|
|
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;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:显示当前进度</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-02-23 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="current">The current.</param>
|
|
/// <param name="total">The total.</param>
|
|
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;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:退出下载程序</para>
|
|
/// <para>创建人:王一帆</para>
|
|
/// <para>创建日期:2020-04-02 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="success">if set to <c>true</c> [success].</param>
|
|
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();
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:取消</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-02-23 </para>
|
|
/// <par |