using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; using System.IO; using System.Threading; using HXX.Scanner.Common; namespace HXX.Scanner.UpdateStarter { class Program { /// /// 程序启动后,复制升级文件到外部,启动外部文件并关闭自身 /// /// static void Main(string[] args) { try { if (System.AppDomain.CurrentDomain.BaseDirectory.Contains("updateTemp")) { Thread.Sleep(1000); int count = 0; bool done = true; while (find_process()) { Thread.Sleep(100); count++; if (count > 50) { LogManager.WriteLog("更新失败,原进程无法结束"); done = false; break; } } if (done) { var from = System.AppDomain.CurrentDomain.BaseDirectory; if (from.EndsWith(@"\")) { from = from.Substring(0, from.Length - 1); } var to = Directory.GetParent(from).FullName; var file = to + @"\" + "HXXScannerClient.exe"; CopyDirectory(from, to); Process proc = new Process(); proc.StartInfo.FileName = file; proc.Start(); } } } catch (Exception ee) { LogManager.WriteLog(ee); } } /// /// 杀掉主程序 /// private static void kill_process() { var pp = Process.GetProcesses(); var count = 0; foreach (var p in pp) { if (p.ProcessName.Contains("HXXScannerClient")) { count++; p.Kill(); } } } /// /// 寻找主程序 /// /// private static bool find_process() { var pp = Process.GetProcesses(); var count = 0; foreach (var p in pp) { if (p.ProcessName.Contains("HXXScannerClient")) { count++; } } if (count > 0) { return true; } else { return false; } } /// /// 拷贝目录 /// /// /// static void CopyDirectory(string srcDir, string tgtDir) { DirectoryInfo source = new DirectoryInfo(srcDir); DirectoryInfo target = new DirectoryInfo(tgtDir); if (target.FullName.StartsWith(source.FullName, StringComparison.CurrentCultureIgnoreCase)) { throw new Exception("父目录不能拷贝到子目录!"); } if (!source.Exists) { return; } if (!target.Exists) { target.Create(); } FileInfo[] files = source.GetFiles(); for (int i = 0; i < files.Length; i++) { if (files[i].FullName.Contains("updateFile")) { continue; } File.Copy(files[i].FullName, target.FullName + @"\" + files[i].Name, true); } DirectoryInfo[] dirs = source.GetDirectories(); for (int j = 0; j < dirs.Length; j++) { CopyDirectory(dirs[j].FullName, target.FullName + @"\" + dirs[j].Name); } } } }