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

797 lines
34 KiB
C#

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.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";
public MainWindow()
{
InitializeComponent();
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]];
}
//foreach (string _Key in ProgressList.Keys)
//{
// _Progress += ProgressList[_Key];
//}
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", "2018/03/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(IsNewSystem))
{
IsNewSystem = "1";
}
Lib.ConfigHelper.UpdateAppConfig(XMLPath, "NewSystem", IsNewSystem);
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(Lib.ConfigHelper.GetAppConfig(XMLPath, "StartApp")))
{
string _StartApp = "TouchCashier.exe|ConnectPoint.exe|GetMembership.exe";
Lib.ConfigHelper.UpdateAppConfig(XMLPath, "StartApp", _StartApp);
}
}
catch { }
}
/// <summary>
/// 主界面显示名称初始化
/// </summary>
private void SoftNameLoading()
{
string _SoftName = "";
try
{
_SoftName = Lib.ConfigHelper.GetAppConfig(XMLPath, "SoftName");
}
catch { }
if (_SoftName == "")
{
_SoftName = "驿商统一收银系统";
Lib.ConfigHelper.UpdateAppConfig(XMLPath, "SoftName", "驿商统一收银系统");
}
switch (_SoftName)
{
case "交投蜀越收银系统":
MessageBeginInvoke("交投蜀越收银系统", "SoftName", "TextBlock");
MessageBeginInvoke("Component/Image/四川.png", "Logo", "Image");
break;
case "高速驿网收银系统":
MessageBeginInvoke("高速驿网收银系统", "SoftName", "TextBlock");
MessageBeginInvoke("Component/Image/浙江.png", "Logo", "Image");
break;
case "贵州公路收银系统":
MessageBeginInvoke("贵州公路收银系统", "SoftName", "TextBlock");
MessageBeginInvoke("Component/Image/贵州公路.png", "Logo", "Image");
break;
default:
MessageBeginInvoke("驿商统一收银系统", "SoftName", "TextBlock");
MessageBeginInvoke("Component/Image/驿商.png", "Logo", "Image");
break;
}
}
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 _ServerXML = "http://" + UpdateUrl + ":" + UpdatePort + "/UpdateServer/UpdateList.xml";
string _WebServiceUri = "http://" + UpdateUrl + ":" + UpdatePort + "/Update.asmx";
//string _WebServiceUri = "http://192.168.11.156:5293/Update.asmx";
XmlDocument _XmlDoc = new XmlDocument();
MessageBeginInvoke("正在检查程序更新......", "txtMsg", "TextBlock");
if (!int.TryParse(UpdateMode, out int _Mode))
{
_Mode = 2;
}
try
{
//使用接口获取更新列表
if (_Mode == 3 || String.IsNullOrWhiteSpace(ServerPartCode) || String.IsNullOrWhiteSpace(ShopCode))
{
System.Collections.Hashtable hashtable = new System.Collections.Hashtable
{
{ "UpdateType", IntConvertToEnumOther(_Mode).ToString() },
{ "UpdateValue", Lib.PCHelper.GetMacAddressByNetworkInformation() }
};
_XmlDoc.LoadXml(Lib.SoapWSHelper.QuerySoapWebServiceString(_WebServiceUri, "POSUpdate", hashtable));
}
else
{
System.Collections.Hashtable hashtable = new System.Collections.Hashtable
{
{ "UpdateType", IntConvertToEnumOther(_Mode).ToString() },
{ "UpdateValue", ServerPartCode + "," + ShopCode }
};
_XmlDoc.LoadXml(Lib.SoapWSHelper.QuerySoapWebServiceString(_WebServiceUri, "POSUpdate", hashtable));
}
}
catch (Exception ex)
{
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");
}
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)])
{
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);
}
//else
//{
// //清空已下载同版本文件
// Lib.FileHelper.DeleteDir(DownPath + "\\" + APPVersion);
// System.Threading.Thread.Sleep(300);
//}
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);
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);
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);
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(FileVersionInfo.GetVersionInfo("TouchCashier.exe").FileVersion))
{
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
}
}