Exemplo n.º 1
0
        public void CopyAndReplace()
        {
            //Directory.Delete("./Update", true);

            var url      = "http://x.newlifex.com/star/staragent50.zip";
            var fileName = Path.GetFileName(url);
            //fileName = "Update".CombinePath(fileName).EnsureDirectory(true);

            var ug = new Upgrade {
                Log = XTrace.Log
            };

            ug.Download(url, fileName);

            // 解压
            var source = ug.Extract(ug.SourceFile);

            // 覆盖
            var dest = "./updateTest";

            ug.CopyAndReplace(source, dest);
        }
Exemplo n.º 2
0
        /// <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);
            }
        }