/// <summary> /// 数据更新 /// </summary> private void DataUpdate(ConnectParam connectParam, ViewModel viewModel, List <UpdateModel> _updateModels) { viewModel.UpdateStatus = "备份原文件..."; try { BackupFile(connectParam, _updateModels); } catch { } viewModel.UpdateStatus = "更新文件..."; try { UpdateFile(connectParam, _updateModels); } catch { viewModel.UpdateStatus = "更新失败,进行还原..."; //还原 ReStoreFile(connectParam, _updateModels); return; } viewModel.UpdateStatus = "清理无效文件..."; try { ClearFile(connectParam); } catch { } }
/// <summary> /// 版本更新 /// </summary> private void VersionUpdate(ConnectParam connectParam, ViewModel viewModel, List <UpdateModel> _updateModels) { viewModel.UpdateStatus = "准备启动安装文件..."; try { Process.Start(connectParam.tempPath + _updateModels[0].FileName); } catch { viewModel.UpdateStatus = "启动安装文件失败..."; } finally { viewModel.UpdateStatus = "等待关闭更新程序..."; Environment.Exit(0); } }
/// <summary> /// 开始更新操作入口点 /// </summary> /// <param name="connectParam"></param> /// <param name="viewModel"></param> public void ConfigUpdate(ConnectParam connectParam, ViewModel viewModel) { updateAccess = new UpdateAccess(connectParam.tempPath + "UdConfig.xml"); _updateModels = updateAccess.GetConfig(); //版本更新 if (_updateModels.Count == 1) { VersionUpdate(connectParam, viewModel, _updateModels); } //数据更新 else { DataUpdate(connectParam, viewModel, _updateModels); } }
/// <summary> /// 清理文件 /// </summary> /// <param name="connectParam"></param> private void ClearFile(ConnectParam connectParam) { try { if (Directory.Exists(currentPath + connectParam.backupPath)) { Directory.Delete(currentPath + connectParam.backupPath, true); } if (Directory.Exists(connectParam.tempPath)) { Directory.Delete(connectParam.tempPath, true); } } catch (Exception ex) { MessageBox.Show("清理异常:" + ex.Message); } }
/// <summary> /// 更新文件 /// </summary> private void UpdateFile(ConnectParam connectParam, List <UpdateModel> updateModels) { foreach (UpdateModel model in updateModels) { try { if (model.MoveToPath.Trim() == "") { File.Copy(connectParam.tempPath + model.FileName, currentPath + model.FileName, true); } else { File.Copy(connectParam.tempPath + model.FileName, currentPath + model.MoveToPath + "\\" + model.FileName, true); } } catch (Exception ex) { MessageBox.Show("更新异常:" + ex.Message); } } }
/// <summary> /// 还原备份 /// </summary> private void ReStoreFile(ConnectParam connectParam, List <UpdateModel> updateModels) { foreach (UpdateModel model in updateModels) { try { //为空,则代表当前目录 if (model.MoveToPath.Trim() == "") { File.Copy(currentPath + connectParam.backupPath + "\\" + model.FileName, currentPath + model.FileName, true); } else { File.Copy(currentPath + connectParam.backupPath + "\\" + model.FileName, currentPath + model.MoveToPath + "\\" + model.FileName, true); } } catch (Exception ex) { MessageBox.Show("还原异常:" + ex.Message); } } }
/// <summary> /// 解压文件 /// </summary> private void DecompressFile(ConnectParam connectParam) { viewModel.UpdateStatus = "开始更新文件..."; Dispatcher.Invoke(new Action(() => { Grid.SetRow(updateStatus, 1); Grid.SetRow(receiveStatus, 3); }), System.Windows.Threading.DispatcherPriority.Normal); viewModel.UpdateStatus = "解压文件..."; try { GZip.Decompress(connectParam.tempPath, connectParam.tempPath, "updatefiles"); viewModel.UpdateStatus = "解压完成..."; } catch (Exception ex) { viewModel.UpdateStatus = "解压出错:" + ex.Message; return; } //备份,更新,还原,清理 DealUpdate dealUpdate = DealUpdate.GetInstance(); dealUpdate.ConfigUpdate(connectParam, viewModel); try { viewModel.UpdateStatus = "登陆程序重新启动中..."; Process.Start(@"G-Box.exe"); } catch { MessageBox.Show("启动程序失败,请手动自行启动!"); } finally { viewModel.UpdateStatus = "等待关闭更新程序..."; Environment.Exit(0); } }
/// <summary> /// 接收数据 /// </summary> /// <param name="connectParam">连接参数</param> private void ReceiveData(ConnectParam connectParam) { Dispatcher.Invoke(new Action(() => { Grid.SetRow(receiveStatus, 1); Grid.SetRow(connStatus, 2); }), System.Windows.Threading.DispatcherPriority.Normal); viewModel.FileName = connectParam.br.ReadString(); //文件名 long fileSize = connectParam.br.ReadInt64(); //文件大小 string fileSize_m = ChangeToSize(fileSize); //转换为MB using (connectParam.fs = new FileStream(connectParam.tempPath + viewModel.FileName, FileMode.Create)) { byte[] buffer = new byte[connectParam.bufferSize]; connectParam.StartTimer(); //开启计时器 while (connectParam.readL < fileSize) { connectParam.size = connectParam.br.Read(buffer, 0, connectParam.bufferSize); connectParam.fs.Write(buffer, 0, connectParam.size); connectParam.readL += connectParam.size; viewModel.FileSize = ChangeToSize(connectParam.readL) + "/" + fileSize_m; if (connectParam.sec != 0) { long downSpeed = connectParam.readL / connectParam.sec; viewModel.DownSpeed = ChangeToSize(downSpeed) + "/S"; //速度 viewModel.LeftTime = new TimeSpan(0, 0, Convert.ToInt32((fileSize - connectParam.readL) / downSpeed)).ToString(); //剩余时间 } viewModel.PgBarValue = String.Format("{0:N1}", (double)connectParam.readL * 100 / fileSize); } } connectParam.StopTimerAndClose(); DecompressFile(connectParam); }
/// <summary> /// 连接服务器异步回调 /// </summary> public void ConnCallBackF(IAsyncResult ar) { TcpClient newClient = (TcpClient)ar.AsyncState; try { newClient.EndConnect(ar); //连接成功 viewModel.ConnectStatus = "连接成功..."; ConnectParam connectParam = new ConnectParam(newClient); CreateTempPath(connectParam.tempPath);//创建缓存路径 viewModel.ConnectStatus = "准备接收文件..."; ReceiveFileDelegate d = new ReceiveFileDelegate(ReceiveData); d.BeginInvoke(connectParam, new AsyncCallback(CallBackF), null); } catch { MessageBox.Show("未能与服务器取得连接!"); } }
/// <summary> /// 备份要更新的文件 /// </summary> private void BackupFile(ConnectParam connectParam, List <UpdateModel> updateModels) { if (!Directory.Exists(currentPath + connectParam.backupPath)) { Directory.CreateDirectory(currentPath + connectParam.backupPath); } foreach (UpdateModel model in updateModels) { try { //为空,则代表当前目录 if (model.MoveToPath.Trim() == "") { File.Copy(currentPath + model.FileName, currentPath + connectParam.backupPath + "\\" + model.FileName, true); } else { File.Copy(currentPath + model.MoveToPath + "\\" + model.FileName, currentPath + connectParam.backupPath + "\\" + model.FileName, true); } } catch (Exception ex) { MessageBox.Show("备份异常:" + ex.Message); } } }