static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { var ex = e.ExceptionObject as Exception; if (ex != null) { MessageBox.Show("发现异常:" + ex.Message); } AutoUpdateCoeus.WriteLogFileAndEmail(e); }
static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) { var ex = e.Exception as Exception; if (ex != null) { MessageBox.Show("发现异常:" + ex.Message); } AutoUpdateCoeus.WriteLogFileAndEmail(e); }
/// <summary> /// 下载文件 /// </summary> /// <param name="currentWorkPath"></param> /// <param name="localPath"></param> /// <param name="ftpUser"></param> /// <param name="ftpPasswd"></param> /// <param name="ftpPath"></param> private void DownLoadFile(string currentWorkPath, string localPath, string ftpUser, string ftpPasswd, string ftpPath, List <FTPFileInfo> fileList) { string curFileName = string.Empty; try { foreach (var fileInfo in fileList) { curFileName = fileInfo.Name; if (fileInfo.IsDirectory) { if (fileInfo.ChildList.Count > 0) { string newFtpPath = (ftpPath.EndsWith("/") ? ftpPath + fileInfo.Name + "/" : ftpPath + "/" + fileInfo.Name + "/"); string newLocalPath = localPath + "\\" + fileInfo.Name; string newCurrentWorkPath = currentWorkPath + "\\" + fileInfo.Name; if (!Directory.Exists(newLocalPath)) { Directory.CreateDirectory(newLocalPath); } DownLoadFile(newCurrentWorkPath, newLocalPath, ftpUser, ftpPasswd, newFtpPath, fileInfo.ChildList); } } else if (fileInfo.NeedDownload) { if (fileInfo.Name == "Newtonsoft.Json.dll") { } ShowInfo(string.Format("正在下载文件{0}...", fileInfo.Name)); int count = 0; while (true) { try { FTPClientHelper.Instance.Download(ftpUser, ftpPasswd, ftpPath, localPath, fileInfo.Name); _hasDownloadSize += fileInfo.Size; break; } catch (Exception ex) { AutoUpdateCoeus.WriteLogFileAndEmail(ex, "下载" + fileInfo.Name + "时发现异常"); count++; if (count <= 3) { ShowInfo(string.Format("下载文件{0}失败!!!失败原因:{1},正在进行第{2}次重试...", fileInfo.Name, ex.Message, count)); continue; } else { ShowInfo(string.Format("下载文件{0}失败!!!失败原因:{1}", fileInfo.Name, ex.Message)); throw ex; } } } ShowInfo(string.Format("下载文件{0}完成", fileInfo.Name)); } } } catch (Exception ex) { AutoUpdateCoeus.WriteLogFileAndEmail(ex, "下载" + curFileName + "时发现异常"); throw; } }
/// <summary> /// 获取文件大小 /// </summary> /// <param name="currentWorkPath">工作路径</param> /// <param name="ftpUser">FTP用户名</param> /// <param name="ftpPasswd">FTP密码</param> /// <param name="ftpPath">FTP的路径</param> private void GetDownloadFileSize(string currentWorkPath, string ftpUser, string ftpPasswd, string ftpPath, List <FTPFileInfo> fileList, string folderName = "") { string tempUpgradeDicectory = appPath + "\\" + AutoUpdateCoeus.TempUpgradeDirectory; string[] fileFullPathList = FTPClientHelper.Instance.GetDetailFilePath(ftpUser, ftpPasswd, ftpPath); List <string> fileNameList = new List <string>(FTPClientHelper.Instance.GetFilePath(ftpUser, ftpPasswd, ftpPath)); foreach (var fileFullPath in fileFullPathList) { string onlyFileName = string.Empty; foreach (var fileName in fileNameList) { if (fileFullPath.Contains(fileName)) { onlyFileName = fileName; break; } } if (!string.IsNullOrEmpty(onlyFileName) && !AutoUpdateCoeus.SelfFiles.Contains(onlyFileName)) { if (fileFullPath.TrimStart().StartsWith("d")) { string newFtpPath = (ftpPath.EndsWith("/") ? ftpPath + onlyFileName + "/" : ftpPath + "/" + onlyFileName + "/"); string newCurrentWorkPath = currentWorkPath + "\\" + onlyFileName; FTPFileInfo fileInfo = new FTPFileInfo { Name = onlyFileName, IsDirectory = true, ChildList = new List <FTPFileInfo>() }; fileList.Add(fileInfo); GetDownloadFileSize(newCurrentWorkPath, ftpUser, ftpPasswd, newFtpPath, fileInfo.ChildList, (!string.IsNullOrEmpty(folderName) ? folderName + "\\" : string.Empty) + onlyFileName); } else { DateTime ftpFileModifyTime = FTPClientHelper.Instance.GetFileModifyDateTime(ftpUser, ftpPasswd, ftpPath, string.Empty, onlyFileName); DateTime localFileModifyTime = ftpFileModifyTime.AddYears(-1); string localFullFileName = currentWorkPath + "\\" + onlyFileName; if (File.Exists(localFullFileName)) { localFileModifyTime = File.GetLastWriteTime(localFullFileName); } //程序路径不存在文件,判断是否存在升级临时文件 if (localFileModifyTime < ftpFileModifyTime) { string tempFullFileName = tempUpgradeDicectory + (!string.IsNullOrEmpty(folderName) ? "\\" + folderName : string.Empty) + "\\" + onlyFileName; if (File.Exists(tempFullFileName)) { localFileModifyTime = File.GetLastWriteTime(tempFullFileName); } } if (onlyFileName.Contains("programConfig") || localFileModifyTime < ftpFileModifyTime) { int count = 0; while (true) { try { var tempList = !string.IsNullOrEmpty(fileFullPath) ? fileFullPath.Split(' ').Where(x => !string.IsNullOrWhiteSpace(x)).ToList() : null; //totalSize += FTPClientHelper.Instance.GetFileSize(ftpUser, ftpPasswd, ftpPath, onlyFileName); if (tempList != null) { _downloadTotalSize += long.Parse(tempList[4]); } this.Invoke(new Action(() => { this.lblProgressVaule.Text = Math.Round((decimal)_hasDownloadSize / 1024 / 1024, 2) + "/" + Math.Round((decimal)_downloadTotalSize / 1024 / 1024, 2) + "M"; })); fileList.Add(new FTPFileInfo { IsDirectory = false, Name = onlyFileName, Size = long.Parse(tempList[4]) }); break; } catch (Exception ex) { AutoUpdateCoeus.WriteLogFileAndEmail(ex, "获取" + onlyFileName + "大小时发现异常"); count++; if (count <= 3) { continue; } else { throw ex; } } } } } fileNameList.Remove(onlyFileName); } } }