Click here to Skip to main content
15,877,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello I have somewhat of a problem. I am trying to execute a MD5 hashing algorithm for a given file but all I have is the directory as a string.

for example I have this as my string..

How can I use the string to locate the file to then calculate the md5 hash of the file and not just the md5 calculation of the string?

thank you in advance!

1 solution

Imports System.Security.Cryptography
Imports System.Text
Imports System.IO

Public Function MD5FileHash(ByVal sFile As String) As String
    Dim MD5 As New MD5CryptoServiceProvider
    Dim Hash As Byte()
    Dim Result As String = ""
    Dim Tmp As String = ""

    Dim FN As New FileStream(sFile, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)

    Hash = MD5.Hash
    Result = Strings.Replace(BitConverter.ToString(Hash), "-", ""
    Return Result
End Function

should do the trick ^^
Share this answer
Draco2013 28-Aug-13 4:47am    
this along with other examples I have found do not return the correct hash of the file. I am testing with the eicar text file witch has a hash of 44D88612FEA8A8F36DE82E1278ABB02F but with your example I get E37D3A9FF32C946E7A3934E8E7DC6345 which I suspect to be the hash of the string of the filename and not the file itself. any help would be greatly appreciated

thank you cheers!
David Goebet 28-Aug-13 5:44am    
if i put a test.pdf in three different locations and let this code run (MD5CryptoServiceProvider("Path\to\my\file.txt")

i get three times the same hash (with three different locations(over 3 Harddrives)
and the other file has another hash so i think its not to wrong
Draco2013 28-Aug-13 4:50am    
the fullpath and the file name are being passed as a string and not within the fileinfo class is this possible?..

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