private void Computer_FileHashComputed(object sender, FileHashInfoEventArgs e) { var fInfo = e.FileHash.FileInfo; Dispatcher.Invoke(() => { var f = filesView.FileItems.FirstOrDefault(x => x.FileInfo == fInfo); if (f != null) { f.SHA256 = FilesHashComputer.ToHexString(e.FileHash.SHA256); f.IsOK = true; } int count = filesView.Count + 1; filesView.Count = count; }); }
private async Task ShowFileDetailsDialog(FileItemView fv) { if (string.IsNullOrEmpty(fv.SHA256) || string.IsNullOrEmpty(fv.PreBytes)) { try { using FileStream fs = fv.FileInfo.Open(FileMode.Open); if (string.IsNullOrEmpty(fv.PreBytes)) { fs.Position = 0; byte[] prebs = new byte[4]; await fs.ReadAsync(prebs, 0, 4); fv.PreBytes = FilesHashComputer.ToHexString(prebs); } if (string.IsNullOrEmpty(fv.SHA256)) { fs.Position = 0; using (SHA256 sha256 = SHA256.Create()) { fv.SHA256 = "Sha256正在计算中..."; byte[] hv = await sha256.ComputeHashAsync(fs); fv.SHA256 = FilesHashComputer.ToHexString(hv); fv.IsOK = true; } } fs.Close(); } catch (UnauthorizedAccessException unex) { fv.SHA256 = "未授权访问"; ShowErrorMsg(unex.Message); } catch (Exception ex) { ShowErrorMsg(ex.Message); } } var fdd = new FileDetailsDialog(fv); fdd.ShowDialog(); }
private async void BtnSearch_Click(object sender, RoutedEventArgs e) { if (string.IsNullOrEmpty(filesView.FolderName)) { return; } if (!Directory.Exists(filesView.FolderName)) { return; } try { filesView.FileHashComputed = false; DirectoryInfo dirInfo = new DirectoryInfo(filesView.FolderName); FileInfo[] files = dirInfo.GetFiles("", SearchOption.AllDirectories); if (!files.Any()) { return; } filesView.Total = files.Length; filesView.FileItems.Clear(); List <FileItemView> fis = new List <FileItemView>(); foreach (var f in files) { string rn = System.IO.Path.GetRelativePath(filesView.FolderName, f.FullName); FileItemView fi = new FileItemView() { FileInfo = f, Name = rn, FullName = f.FullName, Length = f.Length, LastWriteTime = f.LastWriteTime, CreationTime = f.CreationTime, }; fis.Add(fi); filesView.FileItems.Add(fi); } List <FileInfo> hashFiles = new List <FileInfo>(); var lenGroup = fis.GroupBy(x => x.Length); foreach (var g in lenGroup) { if (g.Count() >= 2) { hashFiles.AddRange(g.Select(x => x.FileInfo)); } } int hashCount = hashFiles.Count; filesView.Count = files.Length - hashCount; if (hashCount >= 1) { FilesHashComputer computer = new FilesHashComputer(hashFiles); computer.FileHashComputed += Computer_FileHashComputed; computer.FileHashError += Computer_FileHashError; await computer.ComputeHash(); filesView.FileHashComputed = true; } } catch (Exception ex) { PrintError(ex); } }