Created
February 1, 2026 13:10
-
-
Save wullemsb/1825ad693bcfc1fa363824fa24109106 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System.Security.Cryptography; | |
| using System.Threading.Tasks.Dataflow; | |
| // The recursive block: takes a folder path, returns all entries inside it. | |
| // TransformManyBlock is ideal here because one folder produces many entries. | |
| var getFolderContents = new TransformManyBlock<string, string>(folder => | |
| { | |
| return Directory.GetFileSystemEntries(folder); | |
| }); | |
| // Computes the MD5 hash of a file and pairs it with the filename. | |
| var computeMD5 = new TransformBlock<string, (string FilePath, string Hash)>(filePath => | |
| { | |
| using var md5 = MD5.Create(); | |
| using var stream = File.OpenRead(filePath); | |
| var hashBytes = md5.ComputeHash(stream); | |
| var hash = BitConverter.ToString(hashBytes).Replace("-", ""); | |
| return (filePath, hash); | |
| }); | |
| // Displays the result. | |
| var displayResult = new ActionBlock<(string FilePath, string Hash)>(result => | |
| { | |
| Console.WriteLine($"{result.Hash} {Path.GetFileName(result.FilePath)}"); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment