c82504c18a
SVN-Revision: r1165
251 lines
18 KiB
C#
251 lines
18 KiB
C#
/******************************
|
|
* 说明:自动更新类
|
|
* 创建人:龚宇超
|
|
* 创建日期:2018-02-09
|
|
* 修改人:
|
|
* 修改日期:
|
|
* 修改备注:
|
|
* 版本:1.0.0.0
|
|
******************************/
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Net;
|
|
using System.Xml;
|
|
using System.Xml.Serialization;
|
|
using System.IO;
|
|
using System.Windows.Forms;
|
|
using System.Diagnostics;
|
|
|
|
namespace Lskj.AutoUpdate
|
|
{
|
|
/// <summary>
|
|
/// 自动更新类
|
|
/// </summary>
|
|
public class AutoUpdater : IAutoUpdater
|
|
{
|
|
private bool _isMes = false;
|
|
private bool _needRestart;
|
|
private bool _download;
|
|
private bool _mainLoad;
|
|
public Config _config;
|
|
private List<DownloadFileInfo> _downloadFileListTemp;
|
|
|
|
|
|
|
|
public AutoUpdater()
|
|
{
|
|
_config = new Config().LoadConfig(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConstModel.AUTOUPDATE_CONFIG));
|
|
_mainLoad = false;
|
|
}
|
|
|
|
|
|
public AutoUpdater(string updateURL, bool mainload = true, bool isMes = false,string UpdateNotes="")
|
|
{
|
|
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConstModel.AUTOUPDATE_CONFIG);
|
|
|
|
_config = new Config().LoadConfig(path);
|
|
_config.ServerUrl = updateURL;
|
|
_config.SaveConfig(path);
|
|
_config.UpdateNotes = UpdateNotes;
|
|
_mainLoad = mainload;
|
|
_isMes = isMes;
|
|
}
|
|
|
|
/// <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) && File.Exists(newPath))
|
|
{
|
|
System.IO.File.Copy(oldPath, newPath, true);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:开始下载</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-02-23 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="downloadList">The download list.</param>
|
|
private void StartDownload(List<DownloadFileInfo> downloadList)
|
|
{
|
|
FrmProgress dp = new FrmProgress(downloadList, this);
|
|
if (dp.ShowDialog() == DialogResult.OK)
|
|
{
|
|
if (DialogResult.Cancel == dp.ShowDialog())
|
|
{
|
|
RollBack();
|
|
return;
|
|
}
|
|
_config.SaveConfig(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConstModel.AUTOUPDATE_CONFIG));
|
|
IniUtil.Write("ApplicationUpdateTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm"));
|
|
if (_needRestart)
|
|
{
|
|
Directory.Delete(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConstModel.TEMP_FOLDER_NAME), true);
|
|
|
|
MessageBox.Show(ConstModel.UPDATE_RESTART, ConstModel.LS_TITLE_NAME, MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
CommonUnitity.RestartApplication();
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:Xml 转换为下载集合</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-02-23 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="xml">The XML.</param>
|
|
/// <returns>Dictionary<System.String, RemoteFile>.</returns>
|
|
private Dictionary<string, RemoteFile> ParseRemoteXml(string xml)
|
|
{
|
|
XmlDocument document = new XmlDocument();
|
|
try
|
|
{
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(xml);
|
|
request.Timeout = 10000;
|
|
request.ReadWriteTimeout = 10000;
|
|
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
|
|
{
|
|
using (Stream stream = response.GetResponseStream())
|
|
{
|
|
document.Load(stream);
|
|
}
|
|
}
|
|
}
|
|
catch (WebException ex)
|
|
{
|
|
if (ex.Status == WebExceptionStatus.Timeout)
|
|
{
|
|
throw new Exception("连接超时:网络不稳定或服务器未响应。");
|
|
}
|
|
throw new Exception("网络请求失败: " + ex.Message);
|
|
}
|
|
Dictionary<string, RemoteFile> list = new Dictionary<string, RemoteFile>();
|
|
foreach (XmlNode node in document.DocumentElement.ChildNodes)
|
|
{
|
|
|
|
string key = node.Attributes["path"].Value;
|
|
if (!list.ContainsKey(key))
|
|
list.Add(key, new RemoteFile(node));
|
|
|
|
// 如果节点名字是 UpdateNotes,就取它的内容
|
|
//if (node.Name == "UpdateNotes")
|
|
//{
|
|
// this.UpdateNotes = node.InnerText.Trim();
|
|
//}
|
|
|
|
}
|
|
return list;
|
|
}
|
|
|
|
/// <summary>
|
|
/// <para>说明:更新</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-02-23 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
public void Update()
|
|
{
|
|
Dictionary<string, RemoteFile> listRemotFile = ParseRemoteXml(_config.ServerUrl);
|
|
List<DownloadFileInfo> downloadList = new List<DownloadFileInfo>();
|
|
|
|
foreach (LocalFile file in _config.UpdateFileList)
|
|
{
|
|
if (listRemotFile.ContainsKey(file.Path))
|
|
{
|
|
RemoteFile rf = listRemotFile[file.Path];
|
|
string v1 = rf.Verison;
|
|
string v2 = file.Version;
|
|
if (v1 != v2)
|
|
{
|
|
downloadList.Add(new DownloadFileInfo(rf.Url, rf.Path, rf.LastVer, rf.Size, rf.Verison));
|
|
file.Path = rf.Path;
|
|
file.LastVer = rf.LastVer;
|
|
file.Size = rf.Size;
|
|
file.Version = rf.Verison;
|
|
if (rf.NeedRestart)
|
|
_needRestart = true;
|
|
|
|
_download = true;
|
|
}
|
|
|
|
listRemotFile.Remove(file.Path);
|
|
}
|
|
}
|
|
|
|
foreach (RemoteFile file in listRemotFile.Values)
|
|
{
|
|
downloadList.Add(new DownloadFileInfo(file.Url, file.Path, file.LastVer, file.Size, file.Verison));
|
|
_download = true;
|
|
_config.UpdateFileList.Add(new LocalFile(file.Path, file.LastVer, file.Size, file.Verison));
|
|
if (file.NeedRestart)
|
|
_needRestart = true;
|
|
}
|
|
|
|
_downloadFileListTemp = downloadList;
|
|
if (_download)
|
|
{
|
|
ProcessManager op = new ProcessManager();
|
|
FrmMain dc = new FrmMain(downloadList, _config.UpdateNotes);
|
|
//FrmMain dc = new FrmMain(downloadList);
|
|
if ((downloadList.Count > 0) && (_mainLoad))
|
|
{
|
|
DialogResult dlg = dc.ShowDialog();
|
|
if (dlg != DialogResult.OK)
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (_mainLoad)
|
|
{
|
|
op.StartUpdateProcess(_isMes);
|
|
}
|
|
else
|
|
{
|
|
StartDownload(downloadList);
|
|
}
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:回滚</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2018-02-23 </para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
public void RollBack()
|
|
{
|
|
foreach (DownloadFileInfo file in _downloadFileListTemp)
|
|
{
|
|
string tempUrlPath = CommonUnitity.GetFolderUrl(file);
|
|
string oldPath = string.Empty;
|
|
try
|
|
{
|
|
if (!string.IsNullOrEmpty(tempUrlPath))
|
|
{
|
|
oldPath = Path.Combine(CommonUnitity.SystemBinUrl + tempUrlPath.Substring(1), file.FileName);
|
|
}
|
|
else
|
|
|