Advertisement
Codingale

C# get MD5 of all files in a directory.

May 30th, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.84 KB | None | 0 0
  1.  public string getMD5(string filename) // File name.
  2.         {
  3.             using (var md5 = MD5.Create())
  4.             {
  5.                 using (var stream = File.OpenRead(filename)) // Supports files > 2 GB unlike reading all lines.
  6.                 {
  7.                     return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower();
  8.                     //Creates a "normal" MD5 string.
  9.                 }
  10.             }
  11.         }
  12.  
  13. string[] files = Directory.GetFiles(/* Directory goes here*/); // Gets all files in a folder and puts it into an array.
  14.  
  15. foreach (string file in files) // loop over each file making file = "C:\Wallpaper\image.png" etc
  16. {
  17.         string MD5 = getMD5(file); // See the function definition above.
  18.         // more code here to compare it to a history etc
  19. }
  20. /*
  21.  
  22. There's probably waaaay more efficient ways to do this
  23.  
  24. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement