/// <summary> /// /// </summary> public void ReplaceOperation() { OpreateMainForm(true); bool isReplace = false; ThreadStart thread = delegate { Func <bool> fu = ReplaceFile; isReplace = fu(); if (!isReplace) { Mainthread.BeginInvoke(new Action(() => { DetailText.AppendText("正在重试!"); })); Thread.Sleep(3000); isReplace = fu(); } if (isReplace) { Mainthread.BeginInvoke(new Action(() => { OpreateMainForm(false); })); } else { Mainthread.BeginInvoke(new Action(() => { //BtCancel.IsEnabled = true; //BtOk.IsEnabled = true; UpdateButton.IsEnabled = true; DetailText.AppendText("请重试!"); })); return; } Thread.Sleep(3000); Mainthread.BeginInvoke(new Action(() => { if (isReplace) { Environment.Exit(0); } })); }; try { new Thread(thread).Start(); } catch (Exception ex) { Mainthread.BeginInvoke(new Action(() => { DetailText.AppendText("安装文件失败!\r\n" + ex.Message + "\r\n" + ex.InnerException); })); } }
private void UpdateButton_Click(object sender, RoutedEventArgs e) { UpdateButton.IsEnabled = false; try { WebRequest request = WebRequest.Create(NewDownloadUrl); WebResponse respone = request.GetResponse(); PbProgress.Maximum = respone.ContentLength; if (!Directory.Exists(ThisPath + TmpPath)) { Directory.CreateDirectory(ThisPath + TmpPath); } ThreadPool.QueueUserWorkItem((obj) => { Stream netStream = respone.GetResponseStream(); Stream fileStream = new FileStream(ThisPath + TmpPath + "app.zip", FileMode.Create); byte[] read = new byte[1024]; long progressBarValue = 0; int realReadLen = netStream.Read(read, 0, read.Length); while (realReadLen > 0) { fileStream.Write(read, 0, realReadLen); progressBarValue += realReadLen; PbProgress.Dispatcher.BeginInvoke(new ProgressBarSetter(SetProgressBar), progressBarValue); realReadLen = netStream.Read(read, 0, read.Length); } netStream.Close(); fileStream.Close(); Mainthread.BeginInvoke(new Action(() => { //BtOk.IsEnabled = true; //BtOk.Content = "安装新版"; DetailText.AppendText($@"文件app.zip下载完成!" + "\r\n 准备安装……"); ReplaceOperation(); })); }, null); } catch (Exception ex) { Mainthread.BeginInvoke(new Action(() => { DetailText.AppendText("更新失败!\r\n" + ex.Message + "\r\n" + ex.InnerException); })); } }
private bool ReplaceFile()// 替换文件 { if (!OpreateMainForm(true)) { return(false); } try { string fileName = "app.zip"; if (fileName.Substring(fileName.LastIndexOf('.')).StartsWith(".zip")) { Mainthread.BeginInvoke(new Action(() => { DetailText.AppendText("正在解压替换" + "\r\n"); })); UnZipClass.UnZip(TmpPath + fileName, ThisPath);// 程序的压缩文件,解压操作 Mainthread.BeginInvoke(new Action(() => { DetailText.AppendText("替换完成正在删除临时目录" + "\r\n"); })); Directory.Delete(TmpPath, true); // 删除临时目录; } SaveLocalXml(); return(true); } catch (Exception ex) { Mainthread.BeginInvoke(new Action(() => { DetailText.AppendText("解压替换文件失败!\r\n" + ex.Message + "\r\n" + ex.InnerException); })); return(false); } }