2025-03-28 09:49:56 +08:00

805 lines
35 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.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.ServiceProcess;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using System.Xml;
namespace AutoUpdateEx
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private enum Mode
{
Province,
ServerPart,
Shop,
Machine
}
private enum Service
{
ServiceInstall,
ServiceUpdate
}
private Mode IntConvertToEnumOther(int ModeKey)
{
return (Mode)ModeKey;
}
BackgroundWorker UpdateBackgroundWorker;
Timer UpdateTimer = new Timer();
Dictionary<string, long> ProgressList = new Dictionary<string, long>();
DateTime UpdateStartTime { get; set; }
int FileCount { get; set; }
string UpdateUrl { get; set; }
string UpdatePort { get; set; }
string ServerPartCode { get; set; }
string ShopCode { get; set; }
string UpdateMode { get; set; }
string IsNewSystem { get; set; }
bool UpdateWorkerFlag { get; set; }
bool DownFlag { get; set; }
bool PlayImage { get; set; } = true;
int PlayGroup { get; set; } = 1;
static string AppDir = AppDomain.CurrentDomain.BaseDirectory;
string XMLPath = AppDir + "\\Update.xml";
string DownPath = AppDir + "\\UpdateFiles";
string ServerVersion { get; set; }
public MainWindow()
{
InitializeComponent();
this.Height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
if (this.Height > 768)
{
this.Height = 768;
}
this.Width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
if (this.Width > 1024)
{
this.Width = 1024;
}
Ver.Text = "Ver " + System.Windows.Forms.Application.ProductVersion;
UpdateWorkerFlag = false;
DownFlag = false;
MessageBeginInvoke("0", "downProgress", "ProgressBar");
UpdateStartTime = DateTime.Now;
PlayGroup = RandomNum.GetRandomNum(1, 1, 4)[0];
//SoftNameLoading();
UpdateBackgroundWorker = new BackgroundWorker();
UpdateBackgroundWorker.DoWork += BackgroundWorker_DoWork;
UpdateBackgroundWorker.ProgressChanged += BackgroundWorker_ProgressChanged;
UpdateBackgroundWorker.RunWorkerCompleted += BackgroundWorker_RunWorkerCompleted;
UpdateBackgroundWorker.WorkerReportsProgress = true;
UpdateBackgroundWorker.WorkerSupportsCancellation = true;
UpdateTimer.Elapsed += Timer_Tick;
UpdateTimer.Interval = 100;
UpdateTimer.Start();
}
int imagecount = 0;
private void Timer_Tick(object sender, EventArgs e)
{
if (!UpdateBackgroundWorker.IsBusy && !UpdateWorkerFlag)
{
UpdateWorkerFlag = true;
UpdateBackgroundWorker.RunWorkerAsync();
}
if (ProgressList.Count > 0)
{
long _Progress = 0;
List<string> _KeyList = new List<string>(ProgressList.Keys);
for (int i = 0; i < _KeyList.Count; i++)
{
_Progress += ProgressList[_KeyList[i]];
}
MessageBeginInvoke(((decimal)_Progress / FileCount).ToString("F2"), "downProgress", "ProgressBar");
}
TimeSpan _TimeSpan = DateTime.Now - UpdateStartTime;
if ((int)(_TimeSpan.TotalSeconds) % 3 == 0)
{
if (PlayImage)
{
PlayImage = false;
if (imagecount < 3)
{
imagecount++;
}
else
{
imagecount = 1;
}
MessageBeginInvoke("Component/CarouselFigure/Group" + PlayGroup + "/" + imagecount + ".png", "imgAd", "Image");
}
}
else
{
PlayImage = true;
}
}
/// <summary>
/// 初始化属性
/// </summary>
private void InitializeAttribute()
{
try
{
UpdateUrl = Lib.ConfigHelper.GetAppConfig(XMLPath, "IP");
UpdatePort = Lib.ConfigHelper.GetAppConfig(XMLPath, "Port");
ServerPartCode = Lib.ConfigHelper.GetAppConfig(XMLPath, "ServerPartCode");
ShopCode = Lib.ConfigHelper.GetAppConfig(XMLPath, "ShopCode");
UpdateMode = Lib.ConfigHelper.GetAppConfig(XMLPath, "UpdateMode");
}
catch
{
try
{
if (File.Exists(XMLPath))
{
File.Delete(XMLPath);
}
}
catch { }
}
if (string.IsNullOrWhiteSpace(UpdateUrl) || string.IsNullOrWhiteSpace(UpdatePort) ||
string.IsNullOrWhiteSpace(ServerPartCode) || string.IsNullOrWhiteSpace(ShopCode) ||
string.IsNullOrWhiteSpace(UpdateMode))
{
InitializeConfig();
}
}
/// <summary>
/// 程序配置初始化
/// </summary>
private void InitializeConfig()
{
try
{
if (string.IsNullOrWhiteSpace(UpdateUrl))
{
UpdateUrl = Lib.Win32API.INIGetStringValue(AppDir +
"\\setup.ini", "DBCONECT", "dbip", "");
}
Lib.ConfigHelper.UpdateAppConfig(XMLPath, "IP", UpdateUrl);
if (string.IsNullOrWhiteSpace(UpdatePort))
{
UpdatePort = "11000";
}
Lib.ConfigHelper.UpdateAppConfig(XMLPath, "Port", UpdatePort);
if (string.IsNullOrWhiteSpace(Lib.ConfigHelper.GetAppConfig(XMLPath, "UpDate")))
{
Lib.ConfigHelper.UpdateAppConfig(XMLPath, "UpDate", "2021/11/09 20:33:00");
}
if (string.IsNullOrWhiteSpace(ServerPartCode))
{
ServerPartCode = Lib.Win32API.INIGetStringValue(AppDir +
"\\setup.ini", "DBCONECT", "serverpartcode", "");
}
Lib.ConfigHelper.UpdateAppConfig(XMLPath, "ServerPartCode", ServerPartCode);
if (string.IsNullOrWhiteSpace(ShopCode))
{
ShopCode = Lib.Win32API.INIGetStringValue(AppDir +
"\\setup.ini", "DBCONECT", "shopcode", "");
}
Lib.ConfigHelper.UpdateAppConfig(XMLPath, "ShopCode", ShopCode);
if (string.IsNullOrWhiteSpace(UpdateMode))
{
UpdateMode = "2";
}
Lib.ConfigHelper.UpdateAppConfig(XMLPath, "UpdateMode", UpdateMode);
if (string.IsNullOrWhiteSpace(Lib.ConfigHelper.GetAppConfig(XMLPath, "SoftVersion")))
{
Lib.ConfigHelper.UpdateAppConfig(XMLPath, "SoftVersion",
FileVersionInfo.GetVersionInfo("TouchCashier.exe").FileVersion);
}
if (string.IsNullOrWhiteSpace(Lib.ConfigHelper.GetAppConfig(XMLPath, "SoftName")))
{
Lib.ConfigHelper.UpdateAppConfig(XMLPath, "SoftName",
Lib.Win32API.INIGetStringValue(AppDir +
"\\setup.ini", "DBCONECT", "softwarename", "驿商统一收银系统"));
}
if (string.IsNullOrWhiteSpace(Lib.ConfigHelper.GetAppConfig(XMLPath, "KillApp")))
{
Lib.ConfigHelper.UpdateAppConfig(XMLPath, "KillApp", "TouchCashier.exe|cashier.exe|Stardb.exe|" +
"dbsyc.exe|GetMembership.exe|DataUpdate.exe|InvoicingTool.exe|ConnectPoint.exe");
}
if (string.IsNullOrWhiteSpace(IsNewSystem))
{
IsNewSystem = "1";
}
if (string.IsNullOrWhiteSpace(Lib.ConfigHelper.GetAppConfig(XMLPath, "StartApp")))
{
string _StartApp = "TouchCashier.exe";
//检查收银机屏幕分辨率是否符合新系统最小分辨率要求1024*768
if (SystemParameters.PrimaryScreenWidth < 1024)
{
//不满足最低分辨率要求的老机器使用老的PB版本收银系统
_StartApp = "Stardb.exe|cashier.exe|ConnectPoint.exe";
IsNewSystem = "0";
}
Lib.ConfigHelper.UpdateAppConfig(XMLPath, "StartApp", _StartApp);
}
Lib.ConfigHelper.UpdateAppConfig(XMLPath, "NewSystem", IsNewSystem);
}
catch { }
}
private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//线程报告
}
private void BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//线程退出后操作
}
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
string _ServerDate = null;
string _ServerVersion = null;
string _LocalDate = null;
bool _UpdateFalg = false;
bool _FinishFalg = true;
InitializeAttribute();
//通用更新地址
string _WebServiceUri = "http://" + UpdateUrl + ":" + UpdatePort + "/Update.asmx";
XmlDocument _XmlDoc = new XmlDocument();
MessageBeginInvoke("正在检查程序更新......", "txtMsg", "TextBlock");
try
{
//使用接口获取更新列表
System.Collections.Hashtable hashtable = new System.Collections.Hashtable
{
{ "UpdateType", "Shop" },
{ "UpdateValue", ServerPartCode + "," + ShopCode }
};
if (IsNewSystem == "1")
{
_XmlDoc.LoadXml(Lib.SoapWSHelper.QuerySoapWebServiceString(_WebServiceUri, "POSUpdate", hashtable));
}
else
{
_XmlDoc.LoadXml(Lib.SoapWSHelper.QuerySoapWebServiceString(_WebServiceUri, "POSUpdatePB", hashtable));
}
}
catch (Exception ex)
{
try
{
Lib.LogHelper.WriteServiceLog("接口获取更新版本信息失败:" + ex.Message);
}
catch { }
string _LocalUpdateList = DownPath + "\\Updatelist-" + Lib.ConfigHelper.GetAppConfig(XMLPath, "SoftVersion") + ".xml";
if (File.Exists(_LocalUpdateList))
{
_XmlDoc.Load(_LocalUpdateList);
}
}
try
{
_ServerDate = Lib.XMLHelper.GetXmlAttribute(_XmlDoc, "//UpdateInfo//UpdateTime", "Date").Value;
}
catch (Exception ex)
{
_ServerDate = Lib.ConfigHelper.GetAppConfig(XMLPath, "UpDate");
}
try
{
_ServerVersion = Lib.XMLHelper.GetXmlAttribute(_XmlDoc, "//UpdateInfo//Version", "Num").Value;
}
catch (Exception ex)
{
_ServerVersion = Lib.ConfigHelper.GetAppConfig(XMLPath, "SoftVersion");
}
ServerVersion = _ServerVersion;
try
{
if (!string.IsNullOrEmpty(_ServerDate) && _ServerDate != "")
{
_LocalDate = Lib.ConfigHelper.GetAppConfig(XMLPath, "UpDate");
if (string.IsNullOrEmpty(_LocalDate) || _LocalDate == "")
{
_UpdateFalg = true;
}
else
{
if (DateTime.Compare(Convert.ToDateTime(_ServerDate, CultureInfo.InvariantCulture),
Convert.ToDateTime(_LocalDate, CultureInfo.InvariantCulture)) > 0)
{
_UpdateFalg = true;
}
}
}
DownFlag = true;
if (_UpdateFalg)
{
MessageBeginInvoke("正在创建更新下载任务......", "txtMsg", "TextBlock");
StartDownLoad(_XmlDoc, _ServerVersion, _ServerDate);
}
else
{
try
{
StartKillAPP(_XmlDoc);
}
catch { }
if (StartCheck(_XmlDoc, _ServerVersion, _ServerDate))
{
EndUpdate("收银系统正在启动......");
}
else
{
StartDownLoad(_XmlDoc, _ServerVersion, _ServerDate);
}
}
}
catch
{
try
{
StartKillAPP(_XmlDoc);
}
catch { }
EndUpdate("收银系统正在启动......");
}
}
/// <summary>
/// 关闭已启动程序
/// </summary>
/// <param name="XMLDoc">程序更新文件列表</param>
private bool StartKillAPP(XmlDocument XMLDoc)
{
try
{
string _FileName = "";
XmlNodeList xmlNodeList = XMLDoc.SelectNodes("//UpdateFileList//UpdateFile");
for (int i = 0; i < xmlNodeList.Count; i++)
{
_FileName = xmlNodeList[i].InnerXml.Substring(xmlNodeList[i].InnerXml.LastIndexOf("\\") + 1);
if (_FileName.EndsWith(".rar", true, null))
{
_FileName = _FileName.Substring(0, _FileName.LastIndexOf(".rar"));
}
if (_FileName.EndsWith(".exe", true, null))
{
Lib.ProcessHelper.KillExe(_FileName);
}
}
System.Threading.Thread.Sleep(300);
Lib.ProcessHelper.KillExe(Lib.ConfigHelper.GetAppConfig(XMLPath, "KillApp").Split('|'));
System.Threading.Thread.Sleep(500);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 更新结束启动主程序
/// </summary>
/// <param name="Msg">启动过程显示的消息</param>
private void StartSetupAPP(string Msg)
{
Process[] _ProcessList = Process.GetProcessesByName("dbsrv12");
if (_ProcessList.Count() == 0)
{
try
{
Process.Start(AppDomain.CurrentDomain.BaseDirectory + "dbsrv12.exe",
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "datebase", "hydb.db") +
" -os 10240k -m -c 8m -q -n " + Dns.GetHostName());
}
catch
{ }
}
string[] _APPList = Lib.ConfigHelper.GetAppConfig(XMLPath, "StartApp").Split('|');
for (int i = 0; i < _APPList.Length; i++)
{
decimal _Progress = (decimal)(i + 1) / _APPList.Length * 100;
MessageBeginInvoke(Msg, "txtMsg", "TextBlock");
MessageBeginInvoke(_Progress.ToString("F2"), "downProgress", "ProgressBar");
Lib.ProcessHelper.StartProc(_APPList[i]);
System.Threading.Thread.Sleep(50);
}
//StartProc(_APPList);
}
/// <summary>
/// 检验指定列表文件MD5并进行修复
/// </summary>
/// <param name="XMLDoc">文件列表</param>
/// <param name="APPVersion">程序版本</param>
/// <param name="UpdateDate">更新日期</param>
/// <returns>返回结果</returns>
private bool StartCheck(XmlDocument XMLDoc, string APPVersion, string UpdateDate)
{
string _FileName = "";
XmlNodeList xmlNodeList = XMLDoc.SelectNodes("//UpdateFileList//UpdateFile");
for (int i = 0; i < xmlNodeList.Count; i++)
{
decimal _Progress = (decimal)(i + 1) / xmlNodeList.Count * 100;
MessageBeginInvoke("正在校验程序完整性......(" + _Progress.ToString("F2") + "%)", "txtMsg", "TextBlock");
MessageBeginInvoke(_Progress.ToString("F2"), "downProgress", "ProgressBar");
if (xmlNodeList[i].InnerXml.EndsWith(".rar", true, null))
{
_FileName = xmlNodeList[i].InnerXml.Substring(0, xmlNodeList[i].InnerXml.ToLower().LastIndexOf(".rar"));
}
else
{
_FileName = xmlNodeList[i].InnerXml;
}
//排除TXT文本文件
if (!_FileName.EndsWith(".txt", true, null))
{
//校验启动文件MD5
string _FileMd5 = Lib.FileHelper.GetMD5ByMD5CryptoService(Path.Combine(AppDir, _FileName));
if (string.IsNullOrWhiteSpace(_FileMd5) || _FileMd5 != xmlNodeList[i].Attributes["Md5"].Value)
{
string _FilePath = "";
if (APPVersion == "" || string.IsNullOrEmpty(APPVersion))
{
_FilePath = Path.Combine(DownPath, xmlNodeList[i].InnerXml);
}
else
{
_FilePath = Path.Combine(DownPath, APPVersion, xmlNodeList[i].InnerXml);
}
if (File.Exists(_FilePath))
{
//校验相应版本备份文件MD5
_FileMd5 = Lib.FileHelper.GetMD5ByMD5CryptoService(_FilePath);
if (!string.IsNullOrWhiteSpace(_FileMd5) && _FileMd5 == xmlNodeList[i].Attributes["Md5"].Value)
{
try
{
//备份文件还原
File.Copy(_FilePath, Path.Combine(AppDir, _FileName), true);
}
catch
{
//备份文件还原失败
return false;
}
}
else
{
//启动文件校验失败
return false;
}
}
else
{
//文件不存在,校验失败
return false;
}
}
}
}
//启动文件校验还原成功
return true;
}
/// <summary>
/// 程序更新文件校验
/// </summary>
/// <param name="FileList">文件列表</param>
/// <param name="APPVersion">主程序版本号</param>
/// <param name="UpdateDate">程序更新时间</param>
/// <returns>返回更新结果</returns>
private bool StartCheckUpdate(Dictionary<string, string> FileList, string APPVersion)
{
string[] _Files = Directory.GetFiles(DownPath + "\\" + APPVersion);
for (int i = 0; i < FileList.Count; i++)
{
decimal _Progress = (decimal)(i + 1) / FileList.Count * 100;
MessageBeginInvoke("正在校验更新文件完整性......(" + _Progress.ToString("F0") + "%)", "txtMsg", "TextBlock");
MessageBeginInvoke(_Progress.ToString("F2"), "downProgress", "ProgressBar");
string _FileMd5 = Lib.FileHelper.GetMD5ByMD5CryptoService(DownPath + "\\" + APPVersion + FileList.Keys.ElementAt(i));
if (string.IsNullOrWhiteSpace(_FileMd5) || _FileMd5 != FileList[FileList.Keys.ElementAt(i)])
{
try
{
Lib.LogHelper.WriteServiceLog("更新包文件MD5校验失败。");
}
catch { }
return false;
}
}
return true;
}
/// <summary>
/// 下载更新文件
/// </summary>
/// <param name="XMLDoc">更新文件列表</param>
/// <param name="APPVersion">程序版本</param>
/// <param name="UpdateDate">更新日期</param>
private void StartDownLoad(XmlDocument XMLDoc, string APPVersion, string UpdateDate)
{
bool _FinishFalg = true;
Dictionary<string, string> _FileList = new Dictionary<string, string>();
XmlNodeList xmlNodeList = XMLDoc.SelectNodes("//UpdateFileList//UpdateFile");
for (int i = 0; i < xmlNodeList.Count; i++)
{
_FileList.Add(xmlNodeList[i].InnerXml, xmlNodeList[i].Attributes["Md5"].Value);
}
int _DownEndCount = 0;
if (!string.IsNullOrEmpty(APPVersion))
{
FileCount = _FileList.Count;
//创建更新文件下载进程
for (int i = 0; i < _FileList.Count; i++)
{
//创建版本目录
if (!Directory.Exists(DownPath + "\\" + APPVersion))
{
Directory.CreateDirectory(DownPath + "\\" + APPVersion);
}
string _FileName = _FileList.Keys.ElementAt(i);
string _FileUri = "http://" + UpdateUrl + ":" + UpdatePort + "/UpdateServer" +
(XMLDoc.SelectNodes("//UpdateInfo//Version") == null ? "" : "/" + APPVersion) + "/UpdateFiles" + _FileName.Replace('\\', '/');
string _DirPath = DownPath + (APPVersion == null ? "" : "\\" + APPVersion) + _FileName.Substring(0, _FileName.LastIndexOf("\\") + 1);
if (!Directory.Exists(_DirPath))
{
Directory.CreateDirectory(_DirPath);
}
string _SaveFileName = DownPath + (APPVersion == null ? "" : "\\" + APPVersion) + _FileName;
ProgressList.Add(_FileName, 0);
//验证下载好的系统更新文件MD5确保更新包完整性
if (File.Exists(_SaveFileName) && Lib.FileHelper.GetMD5ByMD5CryptoService(_SaveFileName) == _FileList[_FileName])
{
ProgressList[_FileName] = 100;
_DownEndCount += 1;
if (_DownEndCount >= xmlNodeList.Count)
{
MessageBeginInvoke("更新文件下载完成", "txtMsg", "TextBlock");
MessageBeginInvoke("正在校验更新文件完整性......", "txtMsg", "TextBlock");
if (StartCheckUpdate(_FileList, APPVersion))
{
MessageBeginInvoke("正在安装更新程序......", "txtMsg", "TextBlock");
StartKillAPP(XMLDoc);
try
{
//更新时停止MySQL数据库服务
ServiceController serviceController = ServiceController.GetServices().ToList().Find(p => p.ServiceName.ToUpper() == "MYSQL");
if (serviceController != null)
{
if (serviceController.Status == ServiceControllerStatus.Running)
{
serviceController.Stop();
}
}
}
catch { }
//将更新文件覆盖到程序安装目录下,完成系统更新
if (Lib.FileHelper.CopyOldFilesToNewFiles(Path.Combine(DownPath, APPVersion), AppDir))
{
try
{
//升级成功后,将升级时间和版本号写入更新配置文件
Lib.ConfigHelper.UpdateAppConfig(XMLPath, "UpDate", UpdateDate);
Lib.ConfigHelper.UpdateAppConfig(XMLPath, "SoftVersion", APPVersion);
}
catch { }
EndUpdate("系统更新成功,正在启动收银系统......");
}
else
{
EndUpdate("系统文件占用,本次更新失败,将在下次启动时重试......");
}
}
else
{
EndUpdate("更新文件完整性验证失败,将在下次启动时重试......");
}
}
}
else
{
if (File.Exists(_SaveFileName))
{
try { File.Delete(_SaveFileName); } catch { }
System.Threading.Thread.Sleep(300);
}
if (Lib.HttpHelper.GetHttpLength(_FileUri) == 0)
{
EndUpdate("更新文件完整性验证失败,将在下次启动时重试......");
break;
}
WebClientPro webClient = new WebClientPro(2 * 1000);
webClient.DownloadProgressChanged += delegate (object _sender, DownloadProgressChangedEventArgs Event)
{
//显示下载进度
string _strFileName = _FileName.Substring(_FileName.LastIndexOf("\\") + 1);
_strFileName = _strFileName.Substring(0, _strFileName.LastIndexOf(".rar"));
MessageBeginInvoke("正在下载更新:" + _strFileName + "(" + Lib.FileHelper.ConvertSize(Event.BytesReceived) +
"/" + Lib.FileHelper.ConvertSize(Event.TotalBytesToReceive) + ")", "txtMsg", "TextBlock");
ProgressList[_FileName] = Event.ProgressPercentage;
};
webClient.DownloadFileCompleted += delegate (object _sender, AsyncCompletedEventArgs Event)
{
if (Event.Error != null)
{
_FinishFalg = false;
}
_DownEndCount += 1;
if (_DownEndCount >= xmlNodeList.Count)
{
MessageBeginInvoke("更新文件下载完成", "txtMsg", "TextBlock");
MessageBeginInvoke("正在校验更新文件完整性......", "txtMsg", "TextBlock");
if (StartCheckUpdate(_FileList, APPVersion))
{
MessageBeginInvoke("正在安装更新程序......", "txtMsg", "TextBlock");
StartKillAPP(XMLDoc);
try
{
//更新时停止MySQL数据库服务
ServiceController serviceController = ServiceController.GetServices().ToList().Find(p => p.ServiceName.ToUpper() == "MYSQL");
if (serviceController != null)
{
if (serviceController.Status == ServiceControllerStatus.Running)
{
serviceController.Stop();
}
}
}
catch { }
if (Lib.FileHelper.CopyOldFilesToNewFiles(Path.Combine(DownPath, APPVersion), AppDir))
{
try
{
Lib.ConfigHelper.UpdateAppConfig(XMLPath, "UpDate", UpdateDate);
Lib.ConfigHelper.UpdateAppConfig(XMLPath, "SoftVersion", APPVersion);
}
catch { }
EndUpdate("系统更新成功,正在启动收银系统......");
}
else
{
EndUpdate("系统文件占用,本次更新失败,将在下次启动时重试......");
}
}
else
{
EndUpdate("更新文件完整性验证失败,将在下次启动时重试......");
}
}
};
//创建文件下载线程
webClient.DownloadFileAsync(new Uri(_FileUri), _SaveFileName);
System.Threading.Thread.Sleep(200);
}
}
//保存更新列表文件
XMLDoc.Save(DownPath + "\\UpdateList-" + APPVersion + ".xml");
}
else
{
EndUpdate("正在启动收银系统......");
}
}
/// <summary>
/// 更新完成启动程序
/// </summary>
/// <param name="Msg">更新结果消息</param>
private void EndUpdate(string Msg)
{
try
{
if (Directory.Exists(DownPath))
{
string[] strDirs = Directory.GetDirectories(DownPath);
foreach (string strDir in strDirs)
{
string strDirVersion = strDir.Substring(strDir.LastIndexOf("\\") + 1);
if (Version.TryParse(strDirVersion, out Version version) &&
version < new Version(ServerVersion))
{
Lib.FileHelper.DeleteDir(strDir, true);
if (File.Exists(DownPath + "\\UpdateList-" + strDirVersion + ".xml"))
{
try { File.Delete(DownPath + "\\UpdateList-" + strDirVersion + ".xml"); } catch { }
}
}
}
}
}
catch (Exception ex)
{
try
{
Lib.LogHelper.WriteServiceLog("过期清理失败:" + ex.Message);
}
catch { }
}
MessageBeginInvoke(Msg, "txtMsg", "TextBlock");
StartSetupAPP(Msg);
System.Threading.Thread.Sleep(500);
Environment.Exit(0);
}
#region
/// <summary>
/// 控件线程委托
/// </summary>
/// <param name="Msg">消息内容</param>
/// <param name="ControlName">控件名称</param>
/// <param name="ControlType">控件类型</param>
delegate void DelegateMessage(string Msg, string ControlName, string ControlType);
/// <summary>
/// 线程委托操作事件
/// </summary>
/// <param name="Msg">消息内容</param>
/// <param name="ControlName">控件名称</param>
/// <param name="ControlType">控件类型</param>
private void MessageBeginInvoke(string Msg, string ControlName, string ControlType)
{
Dispatcher.BeginInvoke(new DelegateMessage(MessageHandle), new object[] { Msg, ControlName, ControlType });
}
/// <summary>
/// 线程消息操作
/// </summary>
/// <param name="Msg">消息内容</param>
/// <param name="ControlName">控件名称</param>
/// <param name="ControlsType">控件类型</param>
private void MessageHandle(string Msg, string ControlName, string ControlsType)
{
switch (ControlsType)
{
case "ProgressBar":
if (double.TryParse(Msg, out double _Value))
{
ProgressBar _ProgressBar = (ProgressBar)FindName(ControlName);
_ProgressBar.Value = _Value;
TextBlock _TextValue = (TextBlock)FindName("txt" + ControlName);
double _dTextLeft = _Value / 100 * _ProgressBar.ActualWidth - 5;
_TextValue.Margin = new Thickness(_dTextLeft < _TextValue.ActualWidth ? 0 : _dTextLeft - _TextValue.ActualWidth, 0, 0, 0);
}
break;
case "TextBlock":
TextBlock _TextBlock = (TextBlock)FindName(ControlName);
_TextBlock.Text = Msg;
break;
case "Image":
Image _Image = (Image)FindName(ControlName);
_Image.Source = new BitmapImage(new Uri(Msg, UriKind.Relative));
break;
}
}
#endregion
}
}