using System; using System.Diagnostics; using System.IO; using System.Security.Cryptography; using System.Threading; namespace SMAgentUpdater { class Program { private static string updateFilePath = "../data/agentRecv/"; static void Log(string strLog) { try { using (StreamWriter w = File.AppendText("updatelog.txt")) { var msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "|" + strLog + "\n"; w.WriteLine("{0}", msg); } } catch (Exception ) { } } static void Log(string strFormat, params object[] argvList) { try { string strLog = string.Format(strFormat, argvList); using (StreamWriter w = File.AppendText("updatelog.txt")) { var msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "|" + strLog + "\n"; w.Write("{0}", msg); } } catch (Exception ) { } } static void LogException(Exception ex) { try { Log("catch one exception"); Log(ex.Message); Log(ex.Source); Log(ex.StackTrace); } catch (Exception) { } } static bool IsWindows() { var os = Environment.OSVersion.ToString().ToLowerInvariant(); return os.Contains("windows"); } static string GetExeFileName() { string exename = "SMAgent"; if (IsWindows()) { exename += ".exe"; } return exename; } static bool IsFileExist(string path) { if (File.Exists(path) == false) { Log("file {0} not exist", path); return false; } return true; } // recvFile目录下SMAgent和bin目录下MD5是否一致,如果recvFile新则更新 static bool NeedUpdateAgent() { // 当前运行文件和更新程序在同一层级 var dllFile = "SMAgent.dll"; var pdbFile = "SMAgent.pdb"; // 更新文件位置 var updateDllFile = updateFilePath + "SMAgent.dll"; var updatePdbFile = updateFilePath + "SMAgent.pdb"; if (IsFileExist(dllFile) == false || IsFileExist(pdbFile) == false || IsFileExist(updateDllFile) == false || IsFileExist(updatePdbFile) == false) { Log("file not exist"); return false; } try { string dllMd5, pdbMd5, updateExeMd5, updatePdbMd5; using (var md5 = MD5.Create()) { using (var stream = File.OpenRead(dllFile)) { dllMd5 = BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLowerInvariant(); } using (var stream = File.OpenRead(pdbFile)) { pdbMd5 = BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLowerInvariant(); } using (var stream = File.OpenRead(updateDllFile)) { updateExeMd5 = BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLowerInvariant(); } using (var stream = File.OpenRead(updatePdbFile)) { updatePdbMd5 = BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLowerInvariant(); } } if (dllMd5 != updateExeMd5 || pdbMd5 != updatePdbMd5) { // 暂时用文件生成时间来比较哪个更新一些,用版本号比较的话每次都要修改有点麻烦 if (File.GetCreationTime(dllFile) < File.GetCreationTime(updateDllFile) && File.GetCreationTime(pdbFile) < File.GetCreationTime(updatePdbFile)) { return true; } else { Log("update file is older than current file..."); } } } catch (Exception e) { LogException(e); } return false; } static bool IsProcessRunning(string name) { var proc = Process.GetProcessesByName(name); return proc.Length > 0; } static void StartProcess(string path, string name) { if (IsProcessRunning(name)) { return; } int count = 0; var exeFile = path + "/" + name; Log("start process {0}", exeFile); while (count < 5) { ProcessStartInfo startInfo = new ProcessStartInfo(exeFile, "--id=0.3.1 start &"); startInfo.CreateNoWindow = true; startInfo.WorkingDirectory = path; using (Process proc = new Process()) { proc.StartInfo = startInfo; proc.EnableRaisingEvents = true; proc.Start(); } Thread.Sleep(2000); if (IsProcessRunning(name)) { Log("start succ..."); return; } count++; } } static void Main(string[] args) { try { Log("curr dir {0}, prepare update ...", Directory.GetCurrentDirectory()); if (NeedUpdateAgent() == false) { Log("no need to update ..."); return; } var proc = Process.GetProcessesByName("SMAgent"); foreach (Process p in proc) { p.Kill(); //p.WaitForExit(); } int count = 0; while (IsProcessRunning("SMAgent") && count < 20) { Thread.Sleep(500); count++; } if (! IsProcessRunning("SMAgent")) { File.Copy(updateFilePath + "SMAgent.dll", "SMAgent.dll", true); File.Copy(updateFilePath + "SMAgent.pdb", "SMAgent.pdb", true); StartProcess(Directory.GetCurrentDirectory(), "SMAgent"); } else { Log("stop agent fail"); } } catch (Exception e) { LogException(e); } } } }