using AutoUpdateEx; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Globalization; using System.IO; using System.Linq; using System.Net; using System.Net.NetworkInformation; using System.Text; using System.Windows.Forms; using System.Xml; using System.Xml.Linq; namespace RunUpdateExe { public partial class MainForm : Form { #region 属性 -> 进度显示 WaiteFrom _WaitForm = null; #endregion string strCurFileName = ""; //当前在下载的文件名 private static long SIZE; //总下载大小 private static long UPSIZE; //已经上传多少 private static int NUM; //已经更新件数 private static string[] strArrayFileNames; //需要下载的文件名 private static long lngCurFileSize; private static bool FinishDownLoad = false; string strCurPath = ""; WebClient downWebClient = new WebClient(); private static AutoUpdateEx.lib.ClassCheckProIsRun classCPIR = new AutoUpdateEx.lib.ClassCheckProIsRun(); private static bool boolUpdateFalg = false; #region 方法 -> 构造函数 public MainForm() { InitializeComponent(); ShowWaiteForm(); Init(); } #endregion #region 方法 -> Init() private void Init() { try { //重置数据 SetUpHelper.SetReplaceValue("servername", SetUpHelper.GetMachineName()); SetUpHelper.SetReplaceValue("openpath", ""); //杀死程序 classCPIR.KillExe(ConfigHelper.ConnectPointExe); classCPIR.KillExe(CommonHelper.GetKillAppList); Ping pingSender = new Ping(); PingOptions options = new PingOptions(); options.DontFragment = true; string data = ""; byte[] buffer = Encoding.ASCII.GetBytes(data); int timeout = 1000; PingReply reply = pingSender.Send(CommonHelper.strIP, timeout, buffer, options); if (reply.Status == IPStatus.Success) { //检查更新 _WaitForm.ShowMessage("正在检查更新..."); Application.DoEvents(); checkUpdate(); //更新完成,书写日期 _WaitForm.ShowMessage("检查更新完成..."); Application.DoEvents(); _WaitForm.Visible = true; if (boolUpdateFalg == true) { //杀死进程 classCPIR.KillExe(CommonHelper.GetKillAppList); _WaitForm.ShowMessage("启动更新程序..."); Application.DoEvents(); //直接启动更新--杀死进程 UpdateApplication(); _WaitForm.ShowMessage("正在更新..."); Application.DoEvents(); timer1.Interval = 1000;//一秒钟检索一次 timer1.Tick += timer1_Tick; timer1.Start(); _WaitForm.Visible = true; Application.DoEvents(); } else { StartApplication(); } } else { StartApplication(); } } catch (Exception ex) { StartApplication(); } } /// /// 启动程序 /// private void StartApplication() { try { StartProc(CommonHelper.strStartApp.Split('|')); StartProc(ConfigHelper.ConnectPointExe); } catch { } System.Environment.Exit(0); } void timer1_Tick(object sender, EventArgs e) { if (FinishDownLoad) { StartApplication(); //完成下载,关闭当前进程 System.Environment.Exit(0); } } private void checkUpdate() { string LastUpdate = CommonHelper.strLastUpdateDate; string ServerUpdate = CommonHelper.getServerUpdateTime(); if (!String.IsNullOrEmpty(ServerUpdate) && !String.IsNullOrEmpty(LastUpdate)) //日期都不为空 { if (DateTime.Compare( Convert.ToDateTime(ServerUpdate, CultureInfo.InvariantCulture), Convert.ToDateTime(LastUpdate, CultureInfo.InvariantCulture)) > 0) //字符转日期,并比较日期大小 { boolUpdateFalg = true; //本次更新日期 大于 最近一次更新日期,有更新,修改更新标记 } } } private void UpdateApplication() { //关闭程序 //委托下载数据时事件 downWebClient.DownloadProgressChanged += delegate (object wcsender, DownloadProgressChangedEventArgs ex) { string ShowString = String.Format( CultureInfo.InvariantCulture, "正在下载:{0} [ {1}/{2} ]", strCurFileName, CommonHelper.convertSize(ex.BytesReceived), CommonHelper.convertSize(ex.TotalBytesToReceive)); //呈现内容 lngCurFileSize = ex.TotalBytesToReceive; }; //委托下载完成时事件 downWebClient.DownloadFileCompleted += delegate (object wcsender, AsyncCompletedEventArgs ex) { if (ex.Error != null) { _WaitForm.ShowMessage(ex.Error.Message); _WaitForm.Visible = true; Application.DoEvents(); } else { UPSIZE += lngCurFileSize; if (strArrayFileNames.Length > NUM) { downloadFile(NUM); } else { //更新本地xml CommonHelper.setConfigValue(ConfigHelper.strUpdateXmlPath, "UpDate", CommonHelper.getServerUpdateTime()); //启动程序 FinishDownLoad = true; } } }; SIZE = CommonHelper.getUpdateSize(CommonHelper.strUpdateURL + ConfigHelper.strUpdateListXmlPath); if (SIZE == 0) //判断要更新的文件是否有大小,如果为0字节则结束更新 throw new Exception("更新大小为0"); NUM = 0; UPSIZE = 0; updateList(); string abc = ""; //foreach(string str in strArrayFileNames) //{ // abc += str; //} //MessageBox.Show(abc); if (strArrayFileNames != null) downloadFile(0); } #endregion /// /// 获取文件列表并赋给全局变量 /// private static void updateList() { string xmlPath = CommonHelper.strUpdateURL + ConfigHelper.strUpdateListXmlPath; WebClient wc = new WebClient(); DataSet ds = new DataSet(); ds.Locale = CultureInfo.InvariantCulture; try { StringBuilder sb = new StringBuilder(); XDocument oXDoc = XDocument.Load(xmlPath); System.Xml.Linq.XElement xeRoot = oXDoc.Root; System.Xml.Linq.XElement xele = xeRoot.Element("UpdateFileList"); foreach (var ele in xele.Elements()) { if (sb.Length == 0) { sb.Append(ele.Value); } else { sb.Append("," + ele.Value); } } strArrayFileNames = sb.ToString().Split(','); //赋给全局变量 } catch (WebException ex) { throw ex; } } /// /// 下载文件 /// /// 下载序号 private void downloadFile(int arry) { try { NUM++; //全局变量来记录下载的文件序号 string ReName = ""; strCurFileName = strArrayFileNames[arry]; ReName = strCurFileName; //如果对应的文件经过处理 if (strCurFileName.EndsWith(".rar")) { ReName = strCurFileName.Substring(0, strCurFileName.LastIndexOf(".rar")); //ReName = strCurFileName.Replace(".exe.rar", ".exe").Replace(".pbd.rar", ".pbd"); } string strFileName = strCurFileName.Substring(strCurFileName.LastIndexOf("\\") + 1); //截取文件名 string ReNameFileName = ReName.Substring(ReName.LastIndexOf("\\") + 1); strCurPath = strCurFileName.Substring(0, strCurFileName.LastIndexOf("\\") + 1); //截取文件路径 string StrCurReNamePath = ReName.Substring(0, ReName.LastIndexOf("\\") + 1); string SysDateString = System.DateTime.Now.ToString("yyyyMMdd"); //创建备份日期+更新文件目录 if (!Directory.Exists(ConfigHelper.strBackupPath + SysDateString + StrCurReNamePath)) //路径是否存在 { CommonHelper.createMultipleFolders(ConfigHelper.strBackupPath, SysDateString + StrCurReNamePath); //创建多层目录 } //备份对应的文件-- if (File.Exists(ConfigHelper.strMainProDir + ReName)) { //备份当前时间 if (File.Exists(ConfigHelper.strBackupPath + SysDateString + ReName)) File.Delete(ConfigHelper.strBackupPath + SysDateString + ReName); try { File.Move(ConfigHelper.strMainProDir + ReName, ConfigHelper.strBackupPath + SysDateString + ReName); } catch { } //删除文件 if (File.Exists(ConfigHelper.strMainProDir + ReName)) File.Delete(ConfigHelper.strMainProDir + ReName); } //下载文件--下载到当前路径 downWebClient.DownloadFileAsync(new Uri(CommonHelper.strUpdateURL + ConfigHelper.strSevUpdateFilesUrl + strCurFileName.Replace('\\', '/')), ConfigHelper.strMainProDir + ReName); } catch (WebException ex) { throw ex; } } #region 方法 -> 隐藏窗体 protected override void SetVisibleCore(bool value) { base.SetVisibleCore(false); } #endregion #region 方法 -> 显示等待窗口 public void ShowWaiteForm() { _WaitForm = WaiteFrom.GetInstance(); //设置WaitForm的父窗体为当前窗体,以便在WaitForm中使用父窗体对象 _WaitForm.Owner = this; _WaitForm.ShowMessage("加载中......"); _WaitForm.Show(); Application.DoEvents(); _WaitForm.Refresh(); } #endregion #region 方法 -> 添加对应的收银软件是否开机 private void AddConnectPoint() { foreach (string Str in File.ReadLines("setup.ini")) { if (Str.Contains("=")) { if (Str.Split('=')[0] == "") { } } } //打开文件 //IEnumerable ReadLines(string path); //string OpenPoint = "INSERT INTO T_OPENPOINT" } #endregion #region 方法 -> 连接对应的数据,统计对应的营收 private void GetSumRevenue() { } #endregion public void StartProc(string ProcName) { bool IsRun = false; Process[] processes = Process.GetProcessesByName(ProcName.Substring(0, ProcName.LastIndexOf('.'))); //同程序名的所有进程 foreach (Process p in processes)//判断当前进程中是否已有该程序 { if (p.MainModule.ModuleName == ProcName)//通过程序路径判断,而不能通过程序名判断 { string strVN = p.MainModule.FileVersionInfo.FileVersion; //获取进程中正在运行的这个程序的版本号 p.Kill(); // 结束进程 } } //启动程序 Process.Start(ProcName); } public void StartProc(string[] ProcNames) { foreach (string str in ProcNames) { StartProc(str); } } } }