Beispiel #1
0
        private void OnBtnUnpackClick(object sender, EventArgs e)
        {
            if (filePath != null)
            {
                void Unpack()
                {
                    var dirName = Path.GetFileName(filePath).Split('.').First();

                    TarGZipHelper.Unpack(filePath,
                                         Path.Combine(Path.GetDirectoryName(filePath).ThrowIsNull(nameof(filePath)), dirName),
                                         ShowTarProgressMessage,
                                         new Progress <float>(v =>
                    {
                        void OnReport()
                        {
                            progressBar1.Value = (int)MathF.Floor(v * 100);
                        }
                        Invoke((Action)OnReport);
                    }), maxProgress: 100f);
                    Invoke((Action)(() =>
                    {
                        button3.Enabled = true;
                    }));
                }

                button3.Enabled      = false;
                progressBar1.Value   = 0;
                progressBar1.Maximum = 10000;
                Task.Factory.StartNew(Unpack);
            }
        }
        protected override void OverwriteUpgrade(string value, bool isIncrement)
        {
            if (isIncrement) // 增量更新
            {
                OverwriteUpgradePrivate(value);
            }
            else // 全量更新
            {
                var dirPath = Path.Combine(AppContext.BaseDirectory, Path.GetFileNameWithoutExtension(value));

                if (Directory.Exists(dirPath))
                {
                    Directory.Delete(dirPath, true);
                }

                if (TarGZipHelper.Unpack(value, dirPath, progress: new Progress <float>(OnReportDecompressing), maxProgress: MaxProgressValue))
                {
                    OnReport(MaxProgressValue);
                    IOPath.FileTryDelete(value);
                    OverwriteUpgradePrivate(dirPath);
                }
                else
                {
                    toast.Show(SR.UpdateUnpackFail);
                    OnReport(MaxProgressValue);
                }
            }

            void OverwriteUpgradePrivate(string dirPath)
            {
                OnExit();

                var updateCommandPath = Path.Combine(IOPath.CacheDirectory, "update.cmd");

                IOPath.FileIfExistsItDelete(updateCommandPath);

                var updateCommand = string.Format(
                    SR.ProgramUpdateCmd_,
                    AppHelper.ProgramName,
                    dirPath.TrimEnd(Path.DirectorySeparatorChar),
                    AppContext.BaseDirectory,
                    AppHelper.ProgramPath);

                updateCommand = "chcp" + Environment.NewLine + "chcp 65001" + Environment.NewLine + updateCommand;

                File.WriteAllText(updateCommandPath, updateCommand, Encoding.UTF8);

                using var p                 = new Process();
                p.StartInfo.FileName        = updateCommandPath;
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.CreateNoWindow  = !ThisAssembly.Debuggable; // 不显示程序窗口
                p.StartInfo.Verb            = "runas";                  // 管理员权限运行
                p.Start();                                              // 启动程序
            }
        }