I have a problem while executing a method that computes MD5 and SHA1 checksums for a given file. My methods look like this:
SHA1:
public string HashFile(string toHash)
{
SHA1 SHA1ENGINE = SHA1.Create();
byte[] hashBytes = SHA1ENGINE.ComputeHash(File.Open(toHash, FileMode.Open, FileAccess.Read));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("X2"));
}
return sb.ToString();
}
MD5:
public string HashFile(string toHash)
{
MD5 MD5ENGINE = MD5.Create();
byte[] hashBytes = MD5ENGINE.ComputeHash(File.Open(toHash, FileMode.Open, FileAccess.Read));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("X2"));
}
return sb.ToString();
}
While executing one of these methods I get an IOException: The process cannot access the file '(PATHTOFILE)' because it is being used by another process.
This is a persistent problem, I cannot calculate checksums for any file on my PC. I'm logged as local admin and VS is opened as admin. This is persistent for any directory I've tried for example: C:\TEMP, C:\Users\Administrator\Desktop. I've also tried rebooting my PC, since there actually could be a process that locks files, but that didn't help.
At first I've tried to use:
byte[] hashBytes = SHA1ENGINE.ComputeHash(File.OpenRead(toHash));
But after encountering the described issue and some investigation I've changed that to:
byte[] hashBytes = SHA1ENGINE.ComputeHash(File.Open(toHash, FileMode.Open, FileAccess.Read));
This also didn't help.