/// <summary>开始更新</summary> public void Download() { var link = Link; if (link == null) { throw new Exception("没有可用新版本!"); } if (String.IsNullOrEmpty(link.Url)) { throw new Exception("升级包地址无效!"); } // 如果更新包不存在,则下载 var file = UpdatePath.CombinePath(link.FullName).GetBasePath(); if (!File.Exists(file)) { WriteLog("准备下载 {0} 到 {1}", link.Url, file); var sw = Stopwatch.StartNew(); var web = CreateClient(); TaskEx.Run(() => web.DownloadFileAsync(link.Url, file)).Wait(); sw.Stop(); WriteLog("下载完成!大小{0:n0}字节,耗时{1:n0}ms", file.AsFile().Length, sw.ElapsedMilliseconds); } SourceFile = file; }
/// <summary>开始更新</summary> public void Download() { if (Links.Length == 0) { throw new Exception("没有可用新版本!"); } var link = Links[0]; if (String.IsNullOrEmpty(link.Url)) { throw new Exception("升级包地址无效!"); } // 如果更新包不存在,则下载 var file = UpdatePath.CombinePath(link.Name).GetFullPath(); if (!File.Exists(file)) { WriteLog("准备下载 {0} 到 {1}", link.Url, file); var sw = Stopwatch.StartNew(); var web = CreateClient(); web.DownloadFileAsync(link.Url, file).Wait(); sw.Stop(); WriteLog("下载完成!大小{0:n0}字节,耗时{1:n0}ms", file.AsFile().Length, sw.ElapsedMilliseconds); } // 设置更新标记 file += ".update"; WriteLog("设置更新标记 {0}", file); File.CreateText(file).Close(); }
/// <summary>开始更新</summary> /// <param name="url">下载源</param> /// <param name="fileName">文件名</param> public void Download(String url, String fileName) { // 如果更新包不存在,则下载 var file = UpdatePath.CombinePath(fileName).GetBasePath(); if (!CacheFile && File.Exists(file)) { File.Delete(file); } ; if (!File.Exists(file)) { WriteLog("准备下载 {0} 到 {1}", url, file); var sw = Stopwatch.StartNew(); var web = CreateClient(); Task.Run(() => web.DownloadFileAsync(url, file)).Wait(); sw.Stop(); WriteLog("下载完成!大小{0:n0}字节,耗时{1:n0}ms", file.AsFile().Length, sw.ElapsedMilliseconds); } SourceFile = file; }
/// <summary>开始更新</summary> public virtual async Task <Boolean> Download() { var url = Url; if (url.IsNullOrEmpty()) { return(false); } var fileName = Path.GetFileName(url); // 即使更新包存在,也要下载 var file = UpdatePath.CombinePath(fileName).GetBasePath(); if (File.Exists(file)) { File.Delete(file); } ; WriteLog("准备下载 {0}", url); var sw = Stopwatch.StartNew(); var web = CreateClient(); //await web.DownloadFileAsync(url, file); file = await DownloadFileAsync(web, url, file); sw.Stop(); WriteLog("下载完成!{2} 大小{0:n0}字节,耗时{1:n0}ms", file.AsFile().Length, sw.ElapsedMilliseconds, file); SourceFile = file; return(true); }
/// <summary>检查并执行更新操作</summary> public Boolean Update() { // 查找更新目录 var fis = Directory.GetFiles(UpdatePath, "*.update"); if (fis == null || fis.Length == 0) { return(false); } var file = fis[0].GetFullPath().TrimEnd(".update"); WriteLog("发现更新包 {0},删除所有更新标记文件", file); foreach (var item in fis) { try { File.Delete(item); } catch { } } if (!File.Exists(file)) { return(false); } // 如果已经更新过,则也不再更新 if (File.Exists(file + ".updated")) { return(false); } // 解压更新程序包 if (!file.EndsWithIgnoreCase(".zip")) { return(false); } var p = TempPath.CombinePath(Path.GetFileNameWithoutExtension(file)); WriteLog("解压缩更新包到临时目录 {0}", p); //ZipFile.ExtractToDirectory(file, p); file.AsFile().Extract(p); var updatebat = UpdatePath.CombinePath("update.bat").GetFullPath(); MakeBat(updatebat, p, ".".GetFullPath()); // 执行更新程序包 var si = new ProcessStartInfo(); si.FileName = updatebat; si.WorkingDirectory = Path.GetDirectoryName(si.FileName); if (!XTrace.Debug) { si.CreateNoWindow = true; si.WindowStyle = ProcessWindowStyle.Hidden; } si.UseShellExecute = false; Process.Start(si); WriteLog("已启动更新程序来升级,升级脚本 {0}", updatebat); // 设置更新标记 file += ".updated"; WriteLog("设置已更新标记 {0}", file); File.CreateText(file).Close(); Application.Exit(); return(true); }