/// <summary> /// 从服务器获取文件的hash值 /// </summary> /// <param name="fileName"></param> /// <returns></returns> #region private String getFileHashFromServer(String fileName) private String getFileHashFromServer(String fileName) { String hash = String.Empty; if (String.IsNullOrEmpty(fileName)) { Logger.error(typeof(UpdateController), "file name is empty when try to get hash from server."); return(hash); } hash = update.getHashFromServer(fileName); return(hash); }
/// <summary> /// 开始更新任务 /// </summary> #region public void startUpdate() public void startUpdate() { //检查版本是否更新 for (int i = 0; i < 10; i++)//如果不成功,重复查询十次 { //重复十次查询,没有结果,结束本次更新 if (i >= 10) { Logger.warn(typeof(UpdateController), "check app version for 10 times, can not get anything."); return; } //记录返回的查询结果 String path, name; bool isUpdate = update.checkVersion(out path, out name); //查询结果判断 if (!isUpdate) { Logger.warn(typeof(UpdateController), "the app has not been update in server."); return; } if (String.IsNullOrEmpty(path) || String.IsNullOrEmpty(name)) { Logger.warn(typeof(UpdateController), "update client has find updated version in server, but some error occured unknown."); continue; } serverName = name; serverPath = path; break; } //进行十次下载文件操作,十次以后文件下载如果不成功,则结束本次更新 for (var i = 0; i < 10; i++) { //查询次数大于十次,结束更新 if (i > 9) { Logger.warn(typeof(UpdateController), "download file over 10 times, but still failed,please check if network is avaliable."); return; } //获取本地文件大小 long size = update.checkFileExist(serverName); //开始下载 bool isDownload = update.downloadFile(serverPath, serverName, size); //判断是否下载成功 if (!isDownload) { Logger.warn(typeof(UpdateController), "down load file error. try again."); continue;//下载不成功继续下载 } //下载成功校验sha256值是否正确 String localHash = CommonUtils.getFileSHA256(serverName); String serverHash = update.getHashFromServer(serverName); if (String.IsNullOrEmpty(localHash) || String.IsNullOrEmpty(serverHash) || !localHash.Equals(serverHash)) { Logger.warn(typeof(UpdateController), "download file has been destroyed.delete it and try download again"); CommonUtils.deleteFile(serverName); continue; } Logger.info(typeof(UpdateController), "download file success."); break; } //备份本地数据,十次备份不成功,退出更新 for (var i = 0; i < 10; i++) { if (i > 9)//备份次数大于10,结束备份 { Logger.warn(typeof(UpdateController), "back up local app info error."); return; } String appPath;//记录主程序的运行路径 //开始备份数据 bool isBack = update.backLocalAppInfo(out appPath); if (!isBack)//备份失败 { Logger.warn(typeof(UpdateController), "back up local app info error.try again"); continue; } //备份成功 Logger.info(typeof(UpdateController), "back up local app info success."); installPath = appPath; break; } //安装更新包 update.installApp(installPath, serverName); }