private void BtBuild_Click(object sender, EventArgs e) { Task.Factory.StartNew(() => { this.Invoke(new Action(() => { LbResult.Text = "开始检索并生成目录文件,请稍候……"; })); beginTime = DateTime.Now; string path = TbPath.Text; string parentPath = DirTool.Parent(path); FileCodeHelper fcode = new FileCodeHelper(); if (Directory.Exists(path) && Directory.Exists(parentPath)) { List <string> fileList = FileTool.GetAllFile(path); if (!ListTool.IsNullOrEmpty(fileList)) { VersionModel version = new VersionModel() { Number = DateTime.Now.Second, Path = path, FileList = new List <VersionFile>(), }; foreach (var item in fileList) { version.FileList.Add(new VersionFile() { File = item.Replace(path, ""), MD5 = fcode.GetMD5(item), }); } string file = string.Format(@"{0}\version.txt", parentPath); string json = JsonTool.ToStr(version); TxtTool.Create(file, json); } } endTime = DateTime.Now; this.Invoke(new Action(() => { LbResult.Text = string.Format("生成完成,用时:{0}秒。", (endTime - beginTime).TotalSeconds); })); }); }
/// <summary> /// 下载程序文件 /// </summary> /// <returns></returns> bool DownloadFile(string downloadPath) { if (DirTool.Create(downloadPath)) { FileCodeHelper fcode = new FileCodeHelper(); for (int i = 0; i < version.FileList.Count; i++) { string fileName = Path.GetFileName(version.FileList[i].File); string sourceFile = version.Path + version.FileList[i].File; string destFile = downloadPath + version.FileList[i].File; string destPath = destFile.Substring(0, destFile.Length - fileName.Length); if (DirTool.Create(destPath)) { try { if (File.Exists(AppDir + version.FileList[i].File) && version.FileList[i].MD5 == fcode.GetMD5(AppDir + version.FileList[i].File)) { this.BeginInvoke(new Action(() => { UIDgvFileListUpdate(i, "ColDown", FILE_JUMP); })); } else { //File.Copy(sourceFile, destFile); FtpHelper ftp = new FtpHelper("192.168.31.228", "Administrator", "yuzhengyang"); ftp.DownloadFile(sourceFile, destPath); this.BeginInvoke(new Action(() => { UIDgvFileListUpdate(i, "ColDown", FILE_SUCC); })); } } catch (Exception e) { this.BeginInvoke(new Action(() => { UIDgvFileListUpdate(i, "ColDown", FILE_FAIL); })); } } else { this.BeginInvoke(new Action(() => { UIDgvFileListUpdate(i, "ColDown", FILE_FAIL); })); } Thread.Sleep(WAIT_TIME); } } return(false); }
/// <summary> /// 备份程序文件 /// </summary> /// <returns></returns> bool BackupFile(string backupPath, string downloadPath) { if (DirTool.Create(backupPath)) { FileCodeHelper fcode = new FileCodeHelper(); for (int i = 0; i < version.FileList.Count; i++) { string fileName = Path.GetFileName(version.FileList[i].File); string sourceFile = AppDir + version.FileList[i].File; string destFile = backupPath + version.FileList[i].File; string destPath = destFile.Substring(0, destFile.Length - fileName.Length); string downloadFile = downloadPath + version.FileList[i].File; if (DirTool.Create(destPath)) { try { if (File.Exists(sourceFile) && File.Exists(downloadFile) && version.FileList[i].MD5 != fcode.GetMD5(AppDir + version.FileList[i].File)) { File.Copy(sourceFile, destFile); this.BeginInvoke(new Action(() => { UIDgvFileListUpdate(i, "ColBack", FILE_SUCC); })); } else { this.BeginInvoke(new Action(() => { UIDgvFileListUpdate(i, "ColBack", FILE_JUMP); })); } } catch (Exception e) { this.BeginInvoke(new Action(() => { UIDgvFileListUpdate(i, "ColBack", FILE_FAIL); })); } } else { this.BeginInvoke(new Action(() => { UIDgvFileListUpdate(i, "ColBack", FILE_FAIL); })); } Thread.Sleep(WAIT_TIME); } } return(false); }