| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- 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
- {
- /// <summary>
- /// 程序启动后,复制升级文件到外部,启动外部文件并关闭自身
- /// </summary>
- /// <param name="args"></param>
- 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);
- }
- }
- /// <summary>
- /// 杀掉主程序
- /// </summary>
- 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();
- }
- }
- }
- /// <summary>
- /// 寻找主程序
- /// </summary>
- /// <returns></returns>
- 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;
- }
- }
- /// <summary>
- /// 拷贝目录
- /// </summary>
- /// <param name="srcDir"></param>
- /// <param name="tgtDir"></param>
- 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);
- }
- }
- }
- }
|