Files
lserp_cs_6.0/插件库/Lskj.AutoUpdate/FrmProgress.cs
T
wyf 7708c7ef48 SVN r509
SVN-Revision: r509
2025-04-27 11:09:17 +00:00

481 lines
19 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/******************************
* 说明:下载进度窗口
* 创建人:龚宇超
* 创建日期: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
{
/// <summary>
/// 下载进度窗口
/// </summary>
public partial class FrmProgress : Form
{
/// <summary>
/// 是否下载完成
/// </summary>
private bool isFinished = false;
/// <summary>
/// 下载总数
/// </summary>
private long total = 0;
/// <summary>
/// 当前下载的总数
/// </summary>
private long nDownloadedTotal = 0;
/// <summary>
/// 下载文件列表
/// </summary>
private List<DownloadFileInfo> downloadFileList = new List<DownloadFileInfo>();
/// <summary>
/// 所有文件列表
/// </summary>
private List<DownloadFileInfo> 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<DownloadFileInfo> 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);
}
/// <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)
{
evtDownload = new ManualResetEvent(true);
evtDownload.Reset();
ThreadPool.QueueUserWorkItem(new WaitCallback(this.ProcDownload));
}
/// <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)
{
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();
}
}
/// <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)
{
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();
}
/// <summary>
/// <para>说明:移动文件</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2018-02-23 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="oldPath">The old path.</param>
/// <param name="newPath">The new path.</param>
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);
}
/// <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, 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;
}
}
/// <summary>
/// <para>说明:退出升级程序</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2018-02-23 </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>
/// <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 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);
}
}
/// <summary>
/// <para>说明:下载失败</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2018-02-23 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
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