Пример #1
0
        protected static void CheckAndDownload(String file, String targetPath = null)
        {
            if (!Path.IsPathRooted(file))
            {
                if (!Runtime.IsWeb)
                    file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file);
                else
                    file = Path.Combine(HttpRuntime.BinDirectory, file);
            }

            if (File.Exists(file) && new FileInfo(file).Length > 0) return;

            // 目标目录
            String dir = !String.IsNullOrEmpty(targetPath) ? targetPath : Path.GetDirectoryName(file);

            // 从网上下载文件
            var zipfile = Path.GetFileNameWithoutExtension(file);

            try
            {
                #region 检测64位平台
                Module module = typeof(Object).Module;

                PortableExecutableKinds kind;
                ImageFileMachine machine;
                module.GetPEKind(out kind, out machine);

                if (machine != ImageFileMachine.I386) zipfile += "64";
                #endregion

                zipfile += ".zip";
                String url = String.Format(ServiceAddress, zipfile);

                // 目标Zip文件
                zipfile = Path.Combine(dir, zipfile);
                // Zip文件不存在,准备下载
                if (!File.Exists(zipfile) || new FileInfo(zipfile).Length <= 0)
                {
                    DAL.WriteLog("准备从{0}下载文件到{1}!", url, zipfile);
                    var client = new WebClientX(true, true);
                    // 同步下载,3秒超时
                    client.Timeout = 3000;
                    //var data = client.DownloadData(url);
                    if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
                    client.DownloadFile(url, zipfile);
                    client.Dispose();
                }

                DAL.WriteLog("下载完成,准备解压到{0}!", dir);
                //var ms = new MemoryStream(data);
                //if (file.EndsWith("64")) file = file.Substring(0, file.Length - 2);
                //IOHelper.DecompressFile(ms, dir, file, false);
                ZipFile.Extract(zipfile, dir, true);
                DAL.WriteLog("解压完成!");
            }
            catch (ZipException ex)
            {
                if (File.Exists(zipfile)) File.Delete(zipfile);
                DAL.WriteLog("解压失败,删除压缩文件!{0}", ex.ToString());
            }
            catch (Exception ex)
            {
                DAL.WriteLog(ex.ToString());
            }
        }
Пример #2
0
        void ProcessInternal()
        {
            // 取版本
            // 对比版本
            // 拿出更新源
            // 下载更新包
            // 执行更新

            #region 取版本、对比版本
            WebClientX client = new WebClientX();
            // 同步下载,3秒超时
            client.Timeout = 3000;
            String xml = client.DownloadString(VerSrc);
            if (String.IsNullOrEmpty(xml)) return;

            VerFile ver = new VerFile(xml);
            if (LocalVersion >= ver.GetVersion()) return;
            #endregion

            #region 下载
            String upfile = String.Format("XCoder_{0}.zip", ver.Ver);
            upfile = Path.Combine("Update", upfile);
            String file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, upfile);
            String dir = Path.GetDirectoryName(file);
            if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);

            if (!File.Exists(file))
            {
                String url = ver.Src;
                if (!url.StartsWith("http", StringComparison.OrdinalIgnoreCase))
                {
                    //Uri uri = new Uri(VerSrc);
                    //uri = new Uri(uri, url);
                    //url = uri.ToString();
                    url = VerSrc.Replace("XCoderVer.xml", url);
                }
                XTrace.WriteLine("准备从{0}下载相关文件到{1}!", url, file);

                client.DownloadFile(url, file);
            }
            #endregion

            #region 提示更新
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat("是否更新到最新版本:{0}", ver.Ver);
                sb.AppendLine();
                if (!String.IsNullOrEmpty(ver.Description))
                {
                    sb.AppendLine("更新内容:");
                    sb.Append(ver.Description);
                }

                if (MessageBox.Show(sb.ToString(), "发现新版本", MessageBoxButtons.OKCancel) == DialogResult.Cancel) return;
            }
            #endregion

            #region 更新
            if (File.Exists(file))
            {
                // 解压缩,删除压缩文件
                IOHelper.DecompressFile(file, null, false);
                File.Delete(file);

                StringBuilder sb = new StringBuilder();
                // 复制
                sb.AppendFormat("xcopy {0}\\*.* {1} /s /y /r", dir, AppDomain.CurrentDomain.BaseDirectory);
                sb.AppendLine();
                sb.AppendLine("rd \"" + dir + "\" /s /q");

                // 启动XCoder
                sb.AppendLine("start " + Application.ExecutablePath);
                // 删除dir目录
                sb.AppendLine("rd \"" + dir + "\" /s /q");
                // 删除模版目录
                ////dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Engine.TemplatePath);
                //dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Template");
                //sb.AppendLine("rd \"" + dir + "\" /s /q");

                String tmpfile = Path.GetTempFileName() + ".bat";
                //String tmpfile = "Update_" + DateTime.Now.ToString("yyMMddHHmmss") + ".bat";
                //tmpfile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, tmpfile);
                File.WriteAllText(tmpfile, sb.ToString(), Encoding.Default);

                String ph = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "NewLife.ProcessHelper.exe");
                if (File.Exists(ph)) File.Delete(ph);
                FileSource.ReleaseFile("XCoder.NewLife.ProcessHelper.exe", ph);

                ProcessStartInfo si = new ProcessStartInfo();
                si.FileName = ph;
                si.Arguments = Process.GetCurrentProcess().Id + " " + tmpfile;
                if (!XTrace.Debug)
                {
                    si.CreateNoWindow = true;
                    si.WindowStyle = ProcessWindowStyle.Hidden;
                }
                Process.Start(si);

                //Process.Start(ph, Process.GetCurrentProcess().Id + " " + tmpfile);
                Application.Exit();
            }
            #endregion
        }
Пример #3
0
        protected static void CheckAndDownload(String file, String targetPath = null)
        {
            if (!Path.IsPathRooted(file))
            {
                if (!Runtime.IsWeb)
                    file = file.GetFullPath();
                else
                    file = Path.Combine(HttpRuntime.BinDirectory, file);
            }

            if (File.Exists(file) && new FileInfo(file).Length > 0) return;

            // 目标目录
            String dir = !String.IsNullOrEmpty(targetPath) ? targetPath : Path.GetDirectoryName(file);

            // 从网上下载文件
            var zipfile = Path.GetFileNameWithoutExtension(file);

            try
            {
                #region 检测64位平台
                var module = typeof(Object).Module;

                PortableExecutableKinds kind;
                ImageFileMachine machine;
                module.GetPEKind(out kind, out machine);

                if (machine != ImageFileMachine.I386) zipfile += "64";
                #endregion

                zipfile += ".zip";
                var url = String.Format(ServiceAddress, zipfile);

                var sw = new Stopwatch();
                sw.Start();

                // 检查缓存文件,一个月内有效
                var x = Path.Combine(Path.GetPathRoot(Environment.SystemDirectory), @"X");
                var xfile = Path.Combine(x, zipfile);
                var xf = new FileInfo(xfile);
                if (CacheZip && File.Exists(xfile) && xf.Length > 0 && xf.LastWriteTime.AddMonths(1) > DateTime.Now)
                    zipfile = xfile;
                else
                {
                    // 目标Zip文件
                    zipfile = Path.Combine(dir, zipfile);
                    // Zip文件不存在,准备下载
                    if (!File.Exists(zipfile) || new FileInfo(zipfile).Length <= 0)
                    {
                        DAL.WriteLog("准备从{0}下载文件到{1}!", url, zipfile);
                        var client = new WebClientX(true, true);
                        // 同步下载,3秒超时
                        client.Timeout = 10000;
                        //var data = client.DownloadData(url);
                        if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
                        client.DownloadFile(url, zipfile);
                        client.Dispose();
                    }

                    if (CacheZip) File.Copy(zipfile, xfile, true);
                }
                sw.Stop();
                var size = new FileInfo(zipfile).Length;
                DAL.WriteLog("下载{0}完成({3:n0}字节),耗时{2},准备解压到{1}!", zipfile, dir, sw.Elapsed, size);
                //var ms = new MemoryStream(data);
                //if (file.EndsWith("64")) file = file.Substring(0, file.Length - 2);
                //IOHelper.DecompressFile(ms, dir, file, false);
                ZipFile.Extract(zipfile, dir, true);
                DAL.WriteLog("解压完成!");
            }
            catch (ZipException ex)
            {
                if (File.Exists(zipfile)) File.Delete(zipfile);
                DAL.WriteLog("解压失败,删除压缩文件!{0}", ex.ToString());
            }
            catch (Exception ex)
            {
                DAL.WriteLog(ex.ToString());
            }
        }