Click here to Skip to main content
15,892,737 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to calculate MD5 for all files of a system.

What I have tried:

using System.IO;
using System.Security.Cryptography;

public static class Algorithms
{
    public static readonly HashAlgorithm MD5 = new MD5CryptoServiceProvider();
}

public static string GetChecksum(string filePath, HashAlgorithm algorithm)
{
    using (var stream = new BufferedStream(File.OpenRead(filePath), 100000))
    {
        byte[] hash = algorithm.ComputeHash(stream);
        return BitConverter.ToString(hash).Replace("-", String.Empty);
    }
}


I am using this to calculate for a particular file.

string path = @"C:\Folder\file.txt";

string checksumMd5 = GetChecksum(path, Algorithms.MD5);
Posted
Updated 6-Apr-17 9:18am
v2
Comments
Jochen Arndt 23-Mar-17 7:13am    
Where is the problem?

For recursive file search, search for "c# recursive file search".
To enumerate the drives use the DriveInfo.GetDrives() method.
Then do the recursive file search for each drive starting at the root ("<drive_letter:\>")

But be aware that processing all files will be a time consuming process (it might require days).
kuharan 23-Mar-17 10:23am    
I have an idea to make a list of files using windows commands and put them in a text file. Then read that text file.
Jochen Arndt 23-Mar-17 10:31am    
I still don't get it where you have problems.
kuharan 23-Mar-17 12:37pm    
I dont have problems. I was hoping if there is a quicker way of calculating.
Dave Kreskowiak 6-Apr-17 20:16pm    
Quicker? No. You're still going to be reading every byte of every accessible file in the system. That could be hundreds of thousands to millions of files.

You could get the benefit of threading so you can read multiple files and do the MD5 calculation on the bytes for each, but keep in mind that a disk is a serial device. Only one thread is going to get the data it wants at a time.

But, there is no way to make the calculation on one file go faster. You can only give the illusion of going faster by doing multiple files at once.

1 solution

This is the correct approach, there is no file hashing registry supported by the OS natively
 
Share this answer
 
Comments
[no name] 6-Apr-17 15:38pm    
What?
Matt Comb 7-Apr-17 2:52am    
Neither Windows, NTFS nor FAT calculate a file hash at the time a file is created or modified. Having a Hash registry of this nature is the only way that there would be a faster solution than scanning files and calculating hashes manually.There are some file systems that support it but these are not available for Windows.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900