protected override void OnClosed(System.EventArgs e) { if (installThread != null) { installThread.Abort(); } GlobalEvent.SetUpdateState(false); base.OnClosed(e); }
protected override void ExceptionHandle(Exception e) { Thread.Sleep(1000); //****出现异常后,延迟1秒后,在处理异常 var eType = Utility.SetException(e); switch (eType) { case Utility.EcpType.Normal: break; case Utility.EcpType.Null: break; case Utility.EcpType.AddressError: break; case Utility.EcpType.Disconnect: RundoShowDownloadFileInfo(Utility.E_Disconnect); RundoShowRemainTime(null); RundoShowDownloadSpeed(null); CheckInternet(3); break; case Utility.EcpType.LimitConnect: GlobalData.dataXML.CurrIndex++; doShowDownloadFileInfo(Utility.E_LimitConnect + ":" + GlobalData.dataXML.CurrIndex.ToString() + "号"); System.Windows.Forms.Application.DoEvents(); Utility.Delay(2); ///**********延迟 break; case Utility.EcpType.SerDisconnect: doShowDownloadFileInfo(Utility.E_SerDisconnect); break; case Utility.EcpType.ReceiveError: doShowDownloadFileInfo(Utility.E_ReceiveError); break; case Utility.EcpType.ErrorPw: ftp = new FTPDownload("anonymous", "*****@*****.**"); break; case Utility.EcpType.FileNotFound: //isNewUpdate = true; break; default: //System.Windows.Forms.MessageBox.Show("未知错误"); doShowDownloadFileInfo(e.Message); GlobalEvent.SaveDebugLog(); Environment.Exit(0); break; } }
private static EcpType SetCurrExceptionType(Exception e) { GlobalEvent.WriteLog(e.ToString()); EcpType currEcpType = EcpType.Normal; if (e is System.NotSupportedException || e is System.UriFormatException) { currEcpType = EcpType.AddressError; } else if (e.Message.Contains("无法连接到远程服务器")) { currEcpType = EcpType.SerDisconnect; } else if (e.Message.Contains("421")) { currEcpType = EcpType.LimitConnect; } else if (e.Message.Contains("接收时发生错误")) { currEcpType = EcpType.ReceiveError; } else if (e.Message.Contains("操作超时")) { currEcpType = EcpType.ReceiveError; } else if (e.Message.Contains("远程主机强迫关闭了一个现有的连接")) { currEcpType = EcpType.Disconnect; } else if (e.Message.Contains("331 Password required")) { currEcpType = EcpType.ErrorPw; } else if (e.Message.Contains("文件不可用")) { currEcpType = EcpType.FileNotFound; } else if (e is System.Threading.ThreadAbortException) { currEcpType = EcpType.ThreadEcp; } else { currEcpType = EcpType.Null; } if (!Utility.IsConnectInternetPing()) { currEcpType = EcpType.Disconnect; } GlobalEvent.WriteLog(e.ToString()); GlobalEvent.SaveDebugLog(); return(currEcpType); }
private void InstallConfigData() { if (!Directory.Exists(GlobalData.filePath.ConfigDataPath)) { Directory.CreateDirectory(GlobalData.filePath.ConfigDataPath); } if (File.Exists(GlobalData.filePath.ConfigDataFullPath)) { File.Delete(GlobalData.filePath.ConfigDataFullPath); } GlobalEvent.WriteLog("移动配置文件"); File.Copy(GlobalData.filePath.ConfigDataFullPath_Tmp, GlobalData.filePath.ConfigDataFullPath); }
/// <summary> /// step /// 检测data.config文件是否存在 /// 不存在,则关闭程序 /// </summary> /// <returns></returns> private bool DataConfigExist() { var p = Path.Combine(Directory.GetCurrentDirectory(), "data.config"); if (!File.Exists(p)) { GlobalEvent.WriteLog("没有配置文件。"); System.Windows.Forms.MessageBox.Show("配置文件丢失"); System.Environment.Exit(0); return(false); } return(true); }
void InstallFileBackground() { var t = GlobalData.needUpdateFileInfo; //GlobalData.incompleteFileInfo.Clear(); //for (int i = 0; i < t.Count; i++) //{ // doChangeProgressBarValue(i + 1, t.Count); // if (t[i].FileHash != GlobalEvent.GetMD5Value(t[i].TmpFullPath)) // { // t[i].FileHash = GlobalEvent.GetMD5Value(t[i].TmpFullPath); // GlobalEvent.WriteLog("文件损坏:" + t[i].FileFullName); // GlobalData.incompleteFileInfo.Add(t[i]); // } //} //GlobalData.needUpdateFileInfo.Clear(); if (GlobalData.incompleteFileInfo.Count > 0) { GlobalData.needUpdateFileInfo = GlobalData.incompleteFileInfo; doShowMessageBox(); return; } if (!Directory.Exists(GlobalData.filePath.ConfigDataPath)) { Directory.CreateDirectory(GlobalData.filePath.ConfigDataPath); } if (File.Exists(GlobalData.filePath.ConfigDataFullPath)) { File.Delete(GlobalData.filePath.ConfigDataFullPath); } GlobalEvent.WriteLog("移动配置文件"); File.Move(GlobalData.filePath.ConfigDataFullPath_Tmp, GlobalData.filePath.ConfigDataFullPath); for (int i = 0; i < t.Count; i++) { doChangeProgressBarValue(i + 1, t.Count); FileInfo F = new FileInfo(t[i].MoveFullPath); GlobalEvent.WriteLog("移动第" + i.ToString() + "个文件:" + t[i].FileFullName); if (!Directory.Exists(F.DirectoryName)) { Directory.CreateDirectory(F.DirectoryName); } if (File.Exists(t[i].MoveFullPath)) { File.Delete(t[i].MoveFullPath); } File.Move(t[i].TmpFullPath, t[i].MoveFullPath); Application.DoEvents(); } doInstallOver(); }
private void DeleteRedundant() { for (int i = 0; i < GlobalData.needDeleteFiles.Count; i++) { RundoShowInstallInfo("删除冗沉文件:" + (i + 1).ToString() + "/" + GlobalData.needDeleteFiles.Count.ToString()); GlobalEvent.WriteLog("删除冗沉文件:" + GlobalData.needDeleteFiles[i].Name); RundoChangeProgressBarValue(i + 1, GlobalData.needDeleteFiles.Count); var tp = Path.Combine(GlobalData.filePath.ProgramPath, GlobalData.needDeleteFiles[i].InstallPath); if (File.Exists(tp)) { File.Delete(tp); } } }
/// <summary> /// true: 没有文件损坏 /// false : 有文件损坏 /// 此方法已作废,检测文件放下下载中了。 /// </summary> /// <returns></returns> private bool CheckFiles() { var t = GlobalData.needUpdateFiles; GlobalData.failureFiles = new List <XMLFileInfo>(); for (int i = 0; i < t.Count; i++) { RundoChangeProgressBarValue(i + 1, t.Count); RundoShowInstallInfo("检测文件:" + (i + 1).ToString() + "/" + t.Count.ToString()); GlobalEvent.WriteLog("检测第" + i.ToString() + "个文件:" + t[i].Name); var tP = Path.Combine(GlobalData.filePath.UpdatePath, t[i].Address); var b = (t[i].InstallPath.Contains("Mono") && t[i].InstallPath.Contains("etc")) || (t[i].InstallPath.ToLower().EndsWith(".xml")); if (b) { GlobalEvent.WriteLog("xml文件,跳过检测"); continue; } try { #region 检测文件-----------------------MD5方式 var md5 = Utility.GetMD5Value(tP); if (t[i].Hash != md5) { var tmp = t[i]; GlobalData.failureFiles.Add(tmp); GlobalEvent.WriteLog(tmp.Address); GlobalEvent.WriteLog("云MD5:" + tmp.Hash); GlobalEvent.WriteLog("本地MD5:" + md5); } #endregion } catch { #region 检测文件-------------------文件是否存在方式 因为MD5目前有些电脑会出问题 if (!File.Exists(tP)) { var tmp = t[i]; GlobalData.failureFiles.Add(tmp); } #endregion } } if (GlobalData.failureFiles.Count > 0) { RundoShowMessageBox(); return(false); } return(true); }
void SetInstallOver() { if (this.InvokeRequired) { this.Invoke(new V_F_V(SetInstallOver)); } else { GlobalData.localFileInfo = new XMLAnalysis().GetFileInfomation(GlobalData.filePath.ConfigDataFullPath, true); GlobalEvent.SaveDebugLog(); GlobalEvent.OpenProgram(); GlobalEvent.SetUpdateState(false); Dispose(); } }
public static bool OpenProgram() { if (File.Exists(GlobalData.filePath.ProgramFullPath)) { Process p = new Process(); p.StartInfo.FileName = GlobalData.filePath.ProgramFullPath; p.StartInfo.WorkingDirectory = GlobalData.filePath.ProgramFullPath.Replace(GlobalData.localVersionXML.x_ProcessInfo.name, ""); p.Start(); GlobalEvent.WriteLog("打開程序成功:" + GlobalData.filePath.ProgramFullPath); GlobalEvent.SaveDebugLog(); return(true); } GlobalEvent.WriteLog("打開程序失敗:" + GlobalData.filePath.ProgramFullPath); GlobalEvent.SaveDebugLog(); return(false); }
private void DeleteTmpFile() { var t = GlobalData.needUpdateFiles; for (int i = 0; i < t.Count; i++) { RundoShowInstallInfo("删除临时文件:" + (i + 1).ToString() + "/" + t.Count.ToString()); GlobalEvent.WriteLog("删除临时文件:" + t[i].Name); RundoChangeProgressBarValue(i + 1, t.Count); var tP = Path.Combine(GlobalData.filePath.UpdatePath, t[i].Address); if (File.Exists(tP)) { File.Delete(tP); } } }
private void Install() { var t = GlobalData.needUpdateFiles; ///结束游戏进程 Utility.KillProgram(); InstallBodyFile(); InstallConfigData(); Utility.CopyOData(); RundoShowInstallInfo("安装文件完成"); DeleteTmpFile(); RundoShowInstallInfo("删除冗沉文件"); GlobalEvent.WriteLog("删除冗沉文件"); //DeleteRedundant(); }
/// <summary> /// 下载Version-C.config文件 /// </summary> private void DownloadVersion_C() { if (ftp != null) { ftp.Dispose(); } GlobalEvent.WriteLog("下载version-C.config"); ftp = Utility.CreateFTPDownload(); SetFormUIEvent(); RundoShowDownloadFileInfo("下载配置文件"); if (File.Exists(GlobalData.filePath.ConfigDataFullPath_Tmp)) { File.Delete(GlobalData.filePath.ConfigDataFullPath_Tmp); } if (File.Exists(GlobalData.filePath.ConfigDataFullPath_Tmp + ".zzfz")) { File.Delete(GlobalData.filePath.ConfigDataFullPath_Tmp + ".zzfz"); } while (!ftp.Download(GlobalData.dataXML.CurrVersionAddr, GlobalData.filePath.ConfigDataFullPath_Tmp)) { Utility.SetException(ftp.E); if (File.Exists(GlobalData.filePath.ConfigDataFullPath_Tmp + ".zzfz")) { File.Delete(GlobalData.filePath.ConfigDataFullPath_Tmp + ".zzfz"); } } if (GlobalData.isFirstUse) { var p = Path.Combine(Directory.GetCurrentDirectory(), "data.config.tmp"); if (File.Exists(p)) { File.Delete(p); } while (!ftp.Download(GlobalData.dataXML.CurrDataAddr, p)) { Utility.SetException(ftp.E); } } ftp.Dispose(); }
public UpdateContentForm() { InitializeComponent(); GlobalEvent.SetUpdateState(true); }
private void button1_Click(object sender, EventArgs e) { GlobalEvent.SaveDebugLog(); }
/// <summary> /// 路径要带后缀名 /// </summary> /// <param name="downloadPath"></param> /// <param name="savePath"></param> /// <returns></returns> public bool Download(string downloadPath, string savePath) { try { while (Pause) { continue; } SetProgressBar(0, 1); ShowPercent("0%"); if (savePath.Contains("data.config.tmp")) { GlobalEvent.SaveDebugLog(); } SetProgressBar(0, 1); ShowPercent("0%"); SetProgressBar(0, 1); ShowPercent("0%"); ConnectFTP(downloadPath); long downloadBytes = 0; long TotalBytes = 0; string _path_Tmp = savePath + ".zzfz"; if (File.Exists(savePath)) { SetProgressBar(1, 1); ShowRemainTime("0"); ShowPercent("100%"); ShowDownloadSpeed("已下载"); return(true); } ///断点续传 if (File.Exists(_path_Tmp)) { wrStream = File.OpenWrite(_path_Tmp); downloadBytes = wrStream.Length; wrStream.Seek(downloadBytes, SeekOrigin.Current); request.ContentOffset = downloadBytes; TotalBytes = Size(downloadPath); SetProgressBar((int)downloadBytes, (int)TotalBytes); if (wrStream.Length == TotalBytes) { Dispose(); File.Move(_path_Tmp, savePath); return(true); } } else { var d1 = new FileInfo(_path_Tmp); if (!Directory.Exists(d1.Directory.FullName)) { Directory.CreateDirectory(d1.Directory.FullName); } wrStream = new FileStream(_path_Tmp, FileMode.Create, FileAccess.ReadWrite); TotalBytes = Size(downloadPath); } Stopwatch stopWatch = new Stopwatch(); ShowDownloadSpeed("接收数据中.."); response = (FtpWebResponse)request.GetResponse(); getStream = response.GetResponseStream(); int RemainTime = 0; int n = 1; var bytes = new byte[102400]; stopWatch.Reset(); stopWatch.Start(); int speed_tmp = 0; while (n > 0) { if (Pause) { continue; } n = getStream.Read(bytes, 0, bytes.Length); downloadBytes += n; SetProgressBar((int)downloadBytes, (int)TotalBytes); var f = (float)downloadBytes / (float)TotalBytes; f = f * 100f; ShowPercent(((int)f).ToString() + "%"); speed_tmp += n; if (stopWatch.ElapsedMilliseconds >= 800) { stopWatch.Stop(); DownloadSpeed = speed_tmp * 1000f / stopWatch.ElapsedMilliseconds; ShowDownloadSpeed(DownloadSpeed.ToString()); RemainTime = (int)((TotalBytes - downloadBytes) / DownloadSpeed); ShowRemainTime(RemainTime.ToString()); speed_tmp = 0; stopWatch.Reset(); stopWatch.Start(); } wrStream.Write(bytes, 0, n); } getStream.Dispose(); wrStream.Dispose(); File.Move(_path_Tmp, savePath); Dispose(); return(true); } catch (Exception e) { this.e = e; GlobalEvent.WriteLog(e.Message); Dispose(); return(false); } finally { Dispose(); } }
private void DownADImgs() { if (File.Exists(GlobalData.filePath.ConfigDataFullPath_Tmp)) { File.Delete(GlobalData.filePath.ConfigDataFullPath_Tmp); } if (File.Exists(GlobalData.filePath.ConfigDataFullPath_Tmp + ".zzfz")) { File.Delete(GlobalData.filePath.ConfigDataFullPath_Tmp + ".zzfz"); } var ftp = Utility.CreateFTPDownload(); while (!ftp.Download(GlobalData.dataXML.CurrVersionAddr, GlobalData.filePath.ConfigDataFullPath_Tmp)) { Utility.SetException(ftp.E); if (File.Exists(GlobalData.filePath.ConfigDataFullPath_Tmp + ".zzfz")) { File.Delete(GlobalData.filePath.ConfigDataFullPath_Tmp + ".zzfz"); } } GlobalData.webVersionXML = Utility.Decode <VersionXML>(GlobalData.filePath.ConfigDataFullPath_Tmp); foreach (var item in GlobalData.webVersionXML.x_FileList.x_change) { if (item.Folder == "CompanyLogo") { var logo = item.Files.FindAll(d => d.Address.Contains(GlobalData.adVersion)); var ftpa = Utility.CreateFTPDownload(); foreach (var m in logo) { var dP = Path.Combine(GlobalData.dataXML.CurrIP, m.Address); var nP = Path.Combine(GlobalData.filePath.ProgramPath, m.InstallPath); if (File.Exists(nP)) { if (Utility.GetMD5Value(nP) == m.Hash) { continue; } else { File.Delete(nP); } } if (File.Exists(nP + ".zzfz")) { File.Delete(nP + ".zzfz"); } GlobalEvent.WriteLog("下载广告:" + m.Address); while (!ftpa.Download(dP.Replace("\\", "/"), nP)) { GlobalEvent.WriteLog("下载广告失败:" + m.Address + "-----" + ftpa.E.Message); if (Utility.SetException(ftpa.E) == Utility.EcpType.LimitConnect) { GlobalData.dataXML.CurrIndex++; dP = Path.Combine(GlobalData.dataXML.CurrIP, m.Address); } } } return; } } }
bool GetDifferentFiles() { GlobalData.needUpdateFiles.Clear(); GlobalData.needDeleteFiles.Clear(); Action <List <XMLFileInfo>, List <XMLFileInfo> > _A = (_local, _web) => { var needUpdateFiles = new List <XMLFileInfo>(); var needDeleteFiles = new List <XMLFileInfo>(); needUpdateFiles.AddRange(_web); needDeleteFiles.AddRange(_local); foreach (var l in _local) { foreach (var w in _web) { if (l.Name == w.Name) { if (GlobalData.ignoreXMLFile) { if (l.Name.ToLower().EndsWith(".xml")) { break; } } if (l.Hash == w.Hash) { needUpdateFiles.Remove(w); } break; } } } /// if (needUpdateFiles.Count > 0) { GlobalData.needUpdateFiles.AddRange(needUpdateFiles); } if (needDeleteFiles.Count > 0) { GlobalData.needDeleteFiles.AddRange(needDeleteFiles); } }; _A(GlobalData.localVersionXML.x_FileList.x_base.Files, GlobalData.webVersionXML.x_FileList.x_base.Files); //change文件夹内的文件只在首次使用时下载 //_A(GlobalData.localVersionXML.x_FileList.x_other.Files, GlobalData.webVersionXML.x_FileList.x_other.Files); ///新增的科目 var addNodes = new List <string>(GlobalData.webUpdateNodesName); var needDeletenodes = new List <string>(); foreach (var item in GlobalData.localUpdateNodesName) { ///计算需要删除的节点 if (!GlobalData.webUpdateNodesName.Contains(item)) { //needDeletenodes.Add(item); } else { addNodes.Remove(item); } } foreach (var item in GlobalData.webUpdateNodesName) { var o = new List <XMLFileInfo>(); var n = new List <XMLFileInfo>(); foreach (var i1 in GlobalData.localVersionXML.x_FileList.x_change) { o = null; if (i1.Folder == item) { o = i1.Files; break; } } foreach (var w1 in GlobalData.webVersionXML.x_FileList.x_change) { n = null; if (addNodes.Contains(w1.Folder)) { addNodes.Remove(w1.Folder); GlobalData.needUpdateFiles.AddRange(w1.Files); } if (w1.Folder == item) { n = w1.Files; break; } } if (o != null && n != null) { _A(o, n); } } ///更新车 foreach (var item in GlobalData.webUpdateCarNodesName) { foreach (var w1 in GlobalData.webVersionXML.x_FileList.x_change) { #region 更新车 if (w1.Folder == "CarModel") { foreach (var item1 in w1.Files) { var fol = "\\CarModel\\" + item; var fol2 = "/CarModel/" + item; if (item1.Address.Contains(fol) || item1.Address.Contains(fol2)) { item1.InstallPath = item1.Address.Replace("\\Change\\", "\\"); GlobalData.needUpdateFiles.Add(item1); } } break; } #endregion } } if (GlobalData.isDebug) { GlobalEvent.WriteLog("检测更新: GlobalData.needUpdateFiles :" + GlobalData.needUpdateFiles.Count); foreach (var item in GlobalData.needUpdateFiles) { GlobalEvent.WriteLog(item.Name); } } return(GlobalData.needUpdateFiles.Count > 0); }