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); }
// result.Value: // -1 出错 // 0 不匹配 // 1 匹配 // exception: // 可能会抛出异常 public static NormalResult CheckMD5( this LibraryChannel channel, Stop stop, string strServerFilePath, string strLocalFilePath, delegate_prompt prompt_func) { // stop 对中断 MD5 会起作用 Debug.Assert(stop != null, ""); // 2020/2/26 改为 ...ByTask() // 检查 MD5 // return: // -1 出错 // 0 文件没有找到 // 1 文件找到 int nRet = DynamicDownloader.GetServerFileMD5ByTask( channel, stop, // this.Stop, strServerFilePath, /* * new MessagePromptEventHandler(delegate (object o1, MessagePromptEventArgs e1) * { * //转换为 prompt_func 发生作用 * }), */ (o1, e1) => { if (prompt_func == null) { e1.ResultAction = "cancel"; return; } string[] buttons = e1.Actions.Split(new char[] { ',' }); //转换为 prompt_func 发生作用 e1.ResultAction = prompt_func(channel, e1.MessageText, buttons, 20); }, new System.Threading.CancellationToken(), out byte[] server_md5, out string strError);; if (nRet != 1) { strError = "探测服务器端文件 '" + strServerFilePath + "' MD5 时出错: " + strError; return(new NormalResult(-1, strError)); } using (FileStream stream = File.OpenRead(strLocalFilePath)) { stream.Seek(0, SeekOrigin.Begin); byte[] local_md5 = DynamicDownloader.GetFileMd5(stream); if (ByteArray.Compare(server_md5, local_md5) != 0) { strError = "服务器端文件 '" + strServerFilePath + "' 和本地文件 '" + strLocalFilePath + "' MD5 不匹配"; return(new NormalResult(0, strError)); } } return(new NormalResult(1, null)); }