提交当前本机整理版本
This commit is contained in:
+318
-112
@@ -1,11 +1,11 @@
|
||||
/******************************
|
||||
* 说明:下载进度窗口
|
||||
* 创建人:龚宇超
|
||||
* 创建日期:2018-02-09
|
||||
* 修改人:
|
||||
* 修改日期:
|
||||
* 修改备注:
|
||||
* 版本:1.0.0.0
|
||||
/******************************
|
||||
* 说明:下载进度窗口
|
||||
* 创建人:龚宇超
|
||||
* 创建日期:2018-02-09
|
||||
* 修改人:
|
||||
* 修改日期:
|
||||
* 修改备注:
|
||||
* 版本:1.0.0.0
|
||||
******************************/
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -22,38 +22,45 @@ 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 bool isUserCanceled = false;
|
||||
private bool isClosingByCode = false;
|
||||
private delegate void SetProcessBarCallBack(int current, int total);
|
||||
private delegate void ExitCallBack(bool success);
|
||||
private delegate void ShowCurrentDownloadFileNameCallBack(string name);
|
||||
|
||||
public bool IsUserCanceled
|
||||
{
|
||||
get { return isUserCanceled; }
|
||||
}
|
||||
|
||||
public FrmProgress(List<DownloadFileInfo> downloadFileListTemp, AutoUpdater apd)
|
||||
{
|
||||
InitializeComponent();
|
||||
@@ -65,7 +72,7 @@ namespace Lskj.AutoUpdate
|
||||
|
||||
//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))
|
||||
@@ -73,17 +80,17 @@ namespace Lskj.AutoUpdate
|
||||
// this.downloadFileList.Add(item);
|
||||
// }
|
||||
//}
|
||||
this.lblAll.Text = string.Format("总进度(0/{0})", this.downloadFileList.Count);
|
||||
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>
|
||||
/// <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>
|
||||
@@ -94,40 +101,45 @@ namespace Lskj.AutoUpdate
|
||||
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>
|
||||
/// <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))
|
||||
if (isClosingByCode)
|
||||
{
|
||||
e.Cancel = true;
|
||||
ReleaseDownloadWait();
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (clientDownload != null)
|
||||
clientDownload.CancelAsync();
|
||||
|
||||
evtDownload.Set();
|
||||
evtPerDonwload.Set();
|
||||
if (!isFinished && !isUserCanceled)
|
||||
{
|
||||
if (DialogResult.Yes != MessageBox.Show(ConstModel.UPDATE_GIVING_UP, ConstModel.LS_TITLE_NAME, MessageBoxButtons.YesNo, MessageBoxIcon.Question))
|
||||
{
|
||||
e.Cancel = true;
|
||||
return;
|
||||
}
|
||||
|
||||
CancelUpdate(true);
|
||||
}
|
||||
|
||||
ReleaseDownloadWait();
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:下载文件</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-02-23 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// <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)
|
||||
@@ -148,12 +160,15 @@ namespace Lskj.AutoUpdate
|
||||
|
||||
while (!evtDownload.WaitOne(0, false))
|
||||
{
|
||||
if (isUserCanceled)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
if (this.downloadFileList.Count == 0)
|
||||
break;
|
||||
|
||||
this.lblAll.Text = string.Format("总进度({0}/{1})", count - this.downloadFileList.Count, count);
|
||||
this.lblAll.Text = string.Format("总进度({0}/{1})", count - this.downloadFileList.Count, count);
|
||||
DownloadFileInfo file = this.downloadFileList[0];
|
||||
|
||||
|
||||
@@ -174,7 +189,7 @@ namespace Lskj.AutoUpdate
|
||||
{
|
||||
try
|
||||
{
|
||||
//this.Text = "接收:" + e.BytesReceived.ToString() + " 字节数:" + progressBarCurrent.Maximum.ToString();
|
||||
//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));
|
||||
@@ -191,6 +206,15 @@ namespace Lskj.AutoUpdate
|
||||
{
|
||||
try
|
||||
{
|
||||
if (isUserCanceled || e.Cancelled)
|
||||
{
|
||||
evtPerDonwload.Set();
|
||||
return;
|
||||
}
|
||||
|
||||
if (e.Error != null)
|
||||
throw e.Error;
|
||||
|
||||
DealWithDownloadErrors();
|
||||
DownloadFileInfo dfile = e.UserState as DownloadFileInfo;
|
||||
nDownloadedTotal += dfile.Size;
|
||||
@@ -199,7 +223,13 @@ namespace Lskj.AutoUpdate
|
||||
}
|
||||
catch (Exception exp)
|
||||
{
|
||||
MessageBox.Show("下载错误202:" + exp.Message);
|
||||
if (!isUserCanceled)
|
||||
{
|
||||
DownloadFileInfo dfile = e.UserState as DownloadFileInfo;
|
||||
MessageBox.Show(BuildDownloadErrorMessage(dfile, exp), ConstModel.LS_TITLE_NAME, MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||
evtPerDonwload.Set();
|
||||
ShowErrorAndRestartApplication();
|
||||
}
|
||||
//log the error message,you can use the application's log code
|
||||
}
|
||||
|
||||
@@ -223,6 +253,9 @@ namespace Lskj.AutoUpdate
|
||||
//Wait for the download complete
|
||||
evtPerDonwload.WaitOne();
|
||||
|
||||
if (isUserCanceled)
|
||||
return;
|
||||
|
||||
clientDownload.Dispose();
|
||||
clientDownload = null;
|
||||
|
||||
@@ -231,11 +264,19 @@ namespace Lskj.AutoUpdate
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
MessageBox.Show(this.downloadFileList[0].FileName + "\r\n" + e.Message);
|
||||
ShowErrorAndRestartApplication();
|
||||
if (!isUserCanceled)
|
||||
{
|
||||
DownloadFileInfo file = this.downloadFileList.Count > 0 ? this.downloadFileList[0] : null;
|
||||
MessageBox.Show(BuildDownloadErrorMessage(file, e), ConstModel.LS_TITLE_NAME, MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
||||
ShowErrorAndRestartApplication();
|
||||
}
|
||||
//throw;
|
||||
}
|
||||
}
|
||||
if (isUserCanceled)
|
||||
return;
|
||||
|
||||
|
||||
|
||||
//When the files have not downloaded,return.
|
||||
if (downloadFileList.Count > 0)
|
||||
@@ -247,6 +288,7 @@ namespace Lskj.AutoUpdate
|
||||
DealWithDownloadErrors();
|
||||
|
||||
//Debug.WriteLine("All Downloaded");
|
||||
bool replaceSuccess = true;
|
||||
foreach (DownloadFileInfo file in this.allFileList)
|
||||
{
|
||||
string tempUrlPath = CommonUnitity.GetFolderUrl(file);
|
||||
@@ -269,7 +311,7 @@ namespace Lskj.AutoUpdate
|
||||
System.IO.FileInfo f = new FileInfo(newPath);
|
||||
if (!file.Size.ToString().Equals(f.Length.ToString()) && !file.FileName.ToString().EndsWith(".xml"))
|
||||
{
|
||||
MessageBox.Show($"文件大小与服务器不匹配 "+ file.FileName);
|
||||
MessageBox.Show($"文件大小与服务器不匹配 "+ file.FileName);
|
||||
ShowErrorAndRestartApplication();
|
||||
}
|
||||
|
||||
@@ -320,7 +362,9 @@ namespace Lskj.AutoUpdate
|
||||
}
|
||||
catch (Exception exp)
|
||||
{
|
||||
MessageBox.Show("oldPath:" + oldPath + "\r\n" + "newPath:" + newPath + "\r\n" + exp.Message);
|
||||
replaceSuccess = false;
|
||||
MessageBox.Show("oldPath:" + oldPath + "\r\n" + "newPath:" + newPath + "\r\n" + exp.Message);
|
||||
break;
|
||||
//log the error message,you can use the application's log code
|
||||
}
|
||||
|
||||
@@ -329,7 +373,7 @@ namespace Lskj.AutoUpdate
|
||||
//After dealed with all files, clear the data
|
||||
this.allFileList.Clear();
|
||||
|
||||
if (this.downloadFileList.Count == 0)
|
||||
if (this.downloadFileList.Count == 0 && replaceSuccess)
|
||||
Exit(true);
|
||||
else
|
||||
Exit(false);
|
||||
@@ -337,41 +381,58 @@ namespace Lskj.AutoUpdate
|
||||
evtDownload.Set();
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:移动文件</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-02-23 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// <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)
|
||||
{
|
||||
Exception lastException = null;
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
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"))
|
||||
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);
|
||||
if (File.Exists(oldPath + ".old"))
|
||||
File.Delete(oldPath + ".old");
|
||||
|
||||
if (File.Exists(oldPath))
|
||||
File.Move(oldPath, oldPath + ".old");
|
||||
File.Move(newPath, oldPath);
|
||||
return;
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
lastException = ex;
|
||||
Thread.Sleep(500);
|
||||
}
|
||||
catch (UnauthorizedAccessException ex)
|
||||
{
|
||||
lastException = ex;
|
||||
Thread.Sleep(500);
|
||||
}
|
||||
}
|
||||
|
||||
throw new IOException("文件可能仍被其他进程占用,重试后仍无法替换:" + oldPath, lastException);
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:显示当前下载文件</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-02-23 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// <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)
|
||||
@@ -380,21 +441,21 @@ namespace Lskj.AutoUpdate
|
||||
{
|
||||
ShowCurrentDownloadFileNameCallBack cb = new ShowCurrentDownloadFileNameCallBack(ShowCurrentDownloadFileName);
|
||||
this.Invoke(cb, new object[] { name });
|
||||
this.labelCurrentItem.Text = "正在更新:" + name;
|
||||
this.labelCurrentItem.Text = "正在更新:" + name;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.labelCurrentItem.Text = "正在更新:" + name;
|
||||
this.labelCurrentItem.Text = "正在更新:" + name;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:显示当前进度</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-02-23 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// <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>
|
||||
@@ -412,13 +473,13 @@ namespace Lskj.AutoUpdate
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:退出升级程序</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-02-23 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// <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)
|
||||
@@ -430,40 +491,42 @@ namespace Lskj.AutoUpdate
|
||||
}
|
||||
else
|
||||
{
|
||||
this.isClosingByCode = true;
|
||||
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>
|
||||
/// <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 (isUserCanceled)
|
||||
return;
|
||||
|
||||
if (!isFinished && DialogResult.Yes == MessageBox.Show(ConstModel.UPDATE_GIVING_UP, ConstModel.LS_TITLE_NAME, MessageBoxButtons.YesNo, MessageBoxIcon.Question))
|
||||
{
|
||||
evtDownload.Set();
|
||||
evtPerDonwload.Set();
|
||||
autoUpdater.RollBack();
|
||||
CancelUpdate(true);
|
||||
Exit(false);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:下载失败</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-02-23 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// <para>说明:下载失败</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-02-23 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
private void DealWithDownloadErrors()
|
||||
{
|
||||
@@ -478,4 +541,147 @@ namespace Lskj.AutoUpdate
|
||||
{
|
||||
//MessageBox.Show(e.Message);
|
||||
//log the error message,you can use the application's log code
|
||||
ShowErrorAndRestartApp
|
||||
ShowErrorAndRestartApplication();
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 将WebClient下载异常转换为用户可理解的提示。
|
||||
/// </summary>
|
||||
/// <param name="file">下载文件信息。</param>
|
||||
/// <param name="ex">下载异常。</param>
|
||||
/// <returns>友好的错误提示。</returns>
|
||||
private string BuildDownloadErrorMessage(DownloadFileInfo file, Exception ex)
|
||||
{
|
||||
StringBuilder message = new StringBuilder();
|
||||
message.AppendLine("更新文件下载失败。");
|
||||
if (file != null)
|
||||
{
|
||||
message.AppendLine("文件:" + file.FileName);
|
||||
message.AppendLine("地址:" + file.DownloadUrl);
|
||||
}
|
||||
message.AppendLine("原因:" + GetFriendlyDownloadExceptionMessage(ex));
|
||||
message.AppendLine();
|
||||
message.AppendLine("请检查网络是否正常、更新服务器是否可访问,或关闭防火墙/杀毒软件拦截后重试。");
|
||||
return message.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取下载异常的具体原因。
|
||||
/// </summary>
|
||||
/// <param name="ex">异常对象。</param>
|
||||
/// <returns>异常原因说明。</returns>
|
||||
private string GetFriendlyDownloadExceptionMessage(Exception ex)
|
||||
{
|
||||
WebException webException = ex as WebException;
|
||||
if (webException == null && ex != null)
|
||||
{
|
||||
webException = ex.InnerException as WebException;
|
||||
}
|
||||
|
||||
if (webException != null)
|
||||
{
|
||||
HttpWebResponse response = webException.Response as HttpWebResponse;
|
||||
if (response != null)
|
||||
{
|
||||
if (response.StatusCode == HttpStatusCode.ProxyAuthenticationRequired)
|
||||
{
|
||||
return "代理服务器需要认证,请检查系统代理账号或网络代理设置。";
|
||||
}
|
||||
if (response.StatusCode == HttpStatusCode.NotFound)
|
||||
{
|
||||
return "服务器上找不到该更新文件,请确认更新包是否已上传完整。";
|
||||
}
|
||||
if (response.StatusCode == HttpStatusCode.Forbidden || response.StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
return "服务器拒绝访问该更新文件,请检查文件权限或更新地址配置。";
|
||||
}
|
||||
return string.Format("服务器返回 {0} {1},请确认更新文件是否存在或服务器权限是否正确。", (int)response.StatusCode, response.StatusDescription);
|
||||
}
|
||||
|
||||
switch (webException.Status)
|
||||
{
|
||||
case WebExceptionStatus.NameResolutionFailure:
|
||||
return "无法解析更新服务器地址,请检查DNS、网络连接或服务器地址配置。";
|
||||
case WebExceptionStatus.ConnectFailure:
|
||||
return "无法连接更新服务器,请检查网络、服务器端口或防火墙设置。";
|
||||
case WebExceptionStatus.Timeout:
|
||||
return "连接更新服务器超时,可能是网络不稳定或服务器响应过慢。";
|
||||
case WebExceptionStatus.ProxyNameResolutionFailure:
|
||||
return "代理服务器地址解析失败,请检查系统代理设置。";
|
||||
case WebExceptionStatus.TrustFailure:
|
||||
case WebExceptionStatus.SecureChannelFailure:
|
||||
return "安全连接失败,请检查服务器证书或客户端系统时间。";
|
||||
case WebExceptionStatus.ConnectionClosed:
|
||||
return "下载过程中连接被关闭,可能是网络中断或服务器主动断开。";
|
||||
case WebExceptionStatus.ProtocolError:
|
||||
return "服务器返回了错误响应,请确认更新地址和文件权限。";
|
||||
default:
|
||||
return webException.Message;
|
||||
}
|
||||
}
|
||||
|
||||
if (ex is UriFormatException)
|
||||
{
|
||||
return "更新文件地址格式不正确。";
|
||||
}
|
||||
if (ex is UnauthorizedAccessException)
|
||||
{
|
||||
return "没有写入更新文件的权限,请以管理员身份运行或检查目录权限。";
|
||||
}
|
||||
if (ex is IOException)
|
||||
{
|
||||
return "写入更新文件失败,可能是文件被占用或磁盘空间不足。";
|
||||
}
|
||||
|
||||
return ex != null ? ex.Message : "未知错误。";
|
||||
}
|
||||
/// <summary>
|
||||
/// <para>说明:重新下载</para>
|
||||
/// <para>创建人:龚宇超</para>
|
||||
/// <para>创建日期:2018-02-23 </para>
|
||||
/// <para>修改人:</para>
|
||||
/// <para>修改日期:</para>
|
||||
/// <para>修改备注:</para>
|
||||
/// <para>版本:1.0</para>
|
||||
/// </summary>
|
||||
private void ShowErrorAndRestartApplication()
|
||||
{
|
||||
DialogResult dlg = MessageBox.Show(ConstModel.UPDATE_CONTINUE, ConstModel.LS_TITLE_NAME, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
|
||||
if (dlg == DialogResult.Yes)
|
||||
CommonUnitity.RestartApplication();
|
||||
else
|
||||
{
|
||||
if (clientDownload != null)
|
||||
clientDownload.CancelAsync();
|
||||
ReleaseDownloadWait();
|
||||
autoUpdater.RollBack();
|
||||
|
||||
Exit(false);
|
||||
}
|
||||
}
|
||||
private void CancelUpdate(bool rollback)
|
||||
{
|
||||
isUserCanceled = true;
|
||||
|
||||
if (clientDownload != null)
|
||||
clientDownload.CancelAsync();
|
||||
|
||||
ReleaseDownloadWait();
|
||||
|
||||
if (rollback)
|
||||
autoUpdater.RollBack(true);
|
||||
}
|
||||
|
||||
private void ReleaseDownloadWait()
|
||||
{
|
||||
if (evtDownload != null)
|
||||
evtDownload.Set();
|
||||
|
||||
if (evtPerDonwload != null)
|
||||
evtPerDonwload.Set();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user