private void kernelResTree1_DownloadFiles(object sender, DownloadFilesEventArgs e) { string strError = ""; string strOutputFolder = ""; if (e.Action == "getmd5") { Task.Run(() => { GetMd5(e); }); return; } #if NO List <dp2Circulation.MainForm.DownloadFileInfo> infos = MainForm.BuildDownloadInfoList(e.FileNames); // 询问是否覆盖已有的目标下载文件。整体询问 // return: // -1 出错 // 0 放弃下载 // 1 同意启动下载 int nRet = Program.MainForm.AskOverwriteFiles(infos, // e.FileNames, ref strOutputFolder, out bool bAppend, out strError); if (nRet == -1) { e.ErrorInfo = strError; return; } // return: // -1 出错 // 0 放弃下载 // 1 成功启动了下载 nRet = Program.MainForm.BeginDownloadFiles(infos, // e.FileNames, bAppend ? "append" : "overwrite", null, ref strOutputFolder, out strError); if (nRet == -1) { e.ErrorInfo = strError; } #endif }
void GetMd5(DownloadFilesEventArgs e) { string strError = ""; List <string> lines = new List <string>(); CancellationTokenSource cancel = new CancellationTokenSource(); // 出现一个对话框,允许中断获取 MD5 的过程 FileDownloadDialog dlg = null; this.Invoke((Action)(() => { dlg = new FileDownloadDialog(); dlg.Font = this.Font; dlg.Text = $"正在获取 MD5"; // dlg.SourceFilePath = strTargetPath; dlg.TargetFilePath = null; // 让 Progress 变为走马灯状态 dlg.StartMarquee(); })); dlg.FormClosed += new FormClosedEventHandler(delegate(object o1, FormClosedEventArgs e1) { cancel.Cancel(); }); this.Invoke((Action)(() => { dlg.Show(this); })); var channel = Program.MainForm.GetChannel(this.ServerUrl); // var old_timeout = channel.Timeout; channel.Timeout = new TimeSpan(0, 25, 0); // this.ShowMessage("正在获取 MD5 ..."); try { foreach (string filepath in e.FileNames) { // this.ShowMessage($"正在获取服务器文件 {filepath} 的 MD5 ..."); dlg.SetProgress($"正在获取服务器文件 {filepath} 的 MD5 ...", 0, 0); // 检查 MD5 // return: // -1 出错 // 0 文件没有找到 // 1 文件找到 int nRet = DynamicDownloader.GetServerFileMD5ByTask( channel, null, // this.Stop, filepath, (MessagePromptEventHandler)null, cancel.Token, out byte[] server_md5, out strError); if (nRet != 1) { strError = "探测服务器端文件 '" + filepath + "' MD5 时出错: " + strError; goto ERROR1; } lines.Add($"文件 {filepath} 的 MD5 为 {Convert.ToBase64String(server_md5)}"); } } catch (Exception ex) { strError = ex.Message; goto ERROR1; } finally { //channel.Timeout = old_timeout; Program.MainForm.ReturnChannel(channel); // this.ClearMessage(); this.Invoke((Action)(() => { dlg.Close(); })); cancel.Dispose(); } this.Invoke((Action)(() => { MessageDialog.Show(this, StringUtil.MakePathList(lines, "\r\n")); })); return; ERROR1: ShowMessageBox(strError); }