private void CheckUpdate(Boolean auto) { if (auto) { XTrace.WriteLine("自动更新!"); } var up = new Upgrade(); up.DeleteBackup("."); var cfg = XConfig.Current; if (cfg.LastUpdate.Date < DateTime.Now.Date || !auto) { cfg.LastUpdate = DateTime.Now; cfg.Save(); var root = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); up.Log = XTrace.Log; up.Name = "XCoder"; up.Server = cfg.UpdateServer; up.UpdatePath = root.CombinePath(up.UpdatePath); if (up.Check()) { up.Download(); if (!auto || MessageBox.Show($"发现新版本{up.Link.Time},是否更新?", "自动更新", MessageBoxButtons.YesNo) == DialogResult.Yes) { var rs = up.Update(); MessageBox.Show("更新" + (rs ? "成功" : "失败"), "自动更新"); } } else if (!auto) { if (up.Link != null) { MessageBox.Show($"没有可用更新!最新{up.Link.Time}", "自动更新"); } else { MessageBox.Show("没有可用更新!", "自动更新"); } } } }
/// <summary>获取更新信息</summary> /// <param name="channel"></param> /// <returns></returns> public async Task <UpgradeInfo> Upgrade(String channel) { XTrace.WriteLine("检查更新:{0}", channel); // 清理 var ug = new Upgrade { Log = XTrace.Log }; ug.DeleteBackup("."); var rs = await UpgradeAsync(channel); if (rs != null) { XTrace.WriteLine("发现更新:{0}", rs.ToJson(true)); } return(rs); }
/// <summary>执行更新</summary> /// <param name="ur"></param> /// <returns></returns> public Boolean ProcessUpgrade(UpgradeInfo ur) { XTrace.WriteLine("执行更新:{0} {1}", ur.Version, ur.Source); var dest = "."; var url = ur.Source; try { // 需要下载更新包 if (!url.IsNullOrEmpty()) { var fileName = Path.GetFileName(url); if (!fileName.EndsWithIgnoreCase(".zip")) { fileName = Rand.NextString(8) + ".zip"; } fileName = "Update".CombinePath(fileName).EnsureDirectory(true); // 清理 //NewLife.Net.Upgrade.DeleteBuckup(dest); var ug = new Upgrade { Log = XTrace.Log }; ug.DeleteBackup(dest); // 下载 var sw = Stopwatch.StartNew(); var client = new HttpClient(); client.DownloadFileAsync(url, fileName).Wait(); sw.Stop(); XTrace.WriteLine("下载 {0} 到 {1} 完成,耗时 {2} 。", url, fileName, sw.Elapsed); // 解压 var source = fileName.TrimEnd(".zip"); if (Directory.Exists(source)) { Directory.Delete(source, true); } source.EnsureDirectory(false); fileName.AsFile().Extract(source, true); // 覆盖 ug.CopyAndReplace(source, dest); if (Directory.Exists(source)) { Directory.Delete(source, true); } } // 升级处理命令,可选 var cmd = ur.Executor?.Trim(); if (!cmd.IsNullOrEmpty()) { XTrace.WriteLine("执行更新命令:{0}", cmd); var si = new ProcessStartInfo { UseShellExecute = true, }; var p = cmd.IndexOf(' '); if (p < 0) { si.FileName = cmd; } else { si.FileName = cmd.Substring(0, p); si.Arguments = cmd.Substring(p + 1); } Process.Start(si); } return(true); } catch (Exception ex) { XTrace.WriteLine("更新失败!"); XTrace.WriteException(ex); return(false); } }