zbeng Posted December 9, 2006 Report Share Posted December 9, 2006 I will be providing the screenshot of what ur applications mainform should look in the designer and the full code for now. I am writing up the rest of the tut which contains:1-> What we are extendingWe are extending the above application of creating your own MD5 hash algorithm. We saw how to get the MD5 hash for an invidual individual file in the tutorial above.Now we are going to extend that tutorial to automatically calculate the MD5hashes for all the files in a selected directory.2-> What did we do differentlyNothing much really. The basic logic is along the lines "What do we need to do?"The answer to that question is simplea. Find a way to browse through a directory and collect all the filenamesb. Calculate the hashes for each filec. Write the results into a file that is stored in the directory we just opened with an extension ".MD5Hash"3-> Code snippets.Search a directory for files recursively Dim files As ReadOnlyCollection(Of String) files = My.Computer.FileSystem.GetFiles(Directory as string , SearchOption as Microsoft.VisualBasic.FileIO.SearchOption , paramrry wildcards() as String) The parameters getfiles takes are quite self explanatory. Getfiles returns a ReadOnlyCollection( ) Open a file and write to it Dim filename As String filename = OpenFolderForHashing.SelectedPath & "HashCalc.md5Hash" My.Computer.FileSystem.WriteAllText(filename, "MD5 Hashes for files in this folder" & vbCrLf, True) OpenFolderForHashing -> This the name of the FolderBrowserDialog we drag drop on the form OpenFolderForHashing.SelectedPath -> The path to the folder which we select & -> String concatenation op in VB "HashCalc.md5Hash" -> The default file name for storing our hashes Now, the rest of the code is just looping through the collection for filenames we captured and getting the MD5 Hashes for each individual file in that folder and writing them to a file we createOk! here is the screenshot: Full code needed by the application to run: Imports System.IOImports SystemImports System.Security.CryptographyImports System.TextImports System.Collections.ObjectModelPublic Class HashCalc_MainFrm Private Sub SelectInputFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectInputFile.Click OpenFileForHashing.CheckFileExists = True OpenFileForHashing.CheckPathExists = True OpenFileForHashing.ShowDialog() InputFileName.Text = OpenFileForHashing.FileName Dim fileOpen As FileStream = New FileStream(OpenFileForHashing.FileName, FileMode.Open) ' Create a new instance of the MD5 object. Dim md5Hasher As MD5 = MD5.Create() ' Convert the input string to a byte array and compute the hash. Dim data As Byte() = md5Hasher.ComputeHash(fileOpen) ' Create a new Stringbuilder to collect the bytes ' and create a string. Dim sBuilder As New StringBuilder() ' Loop through each byte of the hashed data ' and format each one as a hexadecimal string. Dim i As Integer For i = 0 To data.Length - 1 sBuilder.Append(data(i).ToString("x2")) Next i fileOpen.Dispose() CalcMD5Hash.Text = sBuilder.ToString().ToUpper End Sub Private Sub HashCalc_MainFrm_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed InputFileName.Text = Nothing CalcMD5Hash.Text = Nothing OpenFileForHashing.Dispose() OpenFolderForHashing.Dispose() End Sub Private Sub SelectDirectory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectDirectory.Click Dim CountFolders As Integer = 0 Dim Iterator As Integer = 0 OpenFolderForHashing.ShowDialog() InputFileName.Text = OpenFolderForHashing.SelectedPath 'Search a directory for files recursively Dim files As ReadOnlyCollection(Of String) files = My.Computer.FileSystem.GetFiles(OpenFolderForHashing.SelectedPath, FileIO.SearchOption.SearchAllSubDirectories, "*.*") CountFolders = files.Count CalcMD5Hash.Text = "Computing hashes......" Dim filename As String filename = OpenFolderForHashing.SelectedPath & "HashCalc.md5Hash" My.Computer.FileSystem.WriteAllText(filename, "MD5 Hashes for files in this folder" & vbCrLf, True) For Iterator = 0 To CountFolders - 1 OpenFileForHashing.FileName = files(Iterator).ToString Dim fileOpen As FileStream = New FileStream(OpenFileForHashing.FileName, FileMode.Open) ' Create a new instance of the MD5 object. Dim md5Hasher As MD5 = MD5.Create() ' Convert the input string to a byte array and compute the hash. Dim data As Byte() = md5Hasher.ComputeHash(fileOpen) ' Create a new Stringbuilder to collect the bytes ' and create a string. Dim sBuilder As New StringBuilder() ' Loop through each byte of the hashed data ' and format each one as a hexadecimal string. Dim i As Integer For i = 0 To data.Length - 1 sBuilder.Append(data(i).ToString("x2")) Next i fileOpen.Dispose() Dim StringToWrite = files(Iterator).ToString & "->" & sBuilder.ToString().ToUpper & vbCrLf My.Computer.FileSystem.WriteAllText(filename, StringToWrite, True) Next Iterator CalcMD5Hash.Text = ".:Completed:." MsgBox("Caclulate hashes stored at:" & vbCrLf & filename, MsgBoxStyle.OkOnly) 'clean up CountFolders = Nothing Iterator = Nothing files = Nothing filename = Nothing End SubHere is a copy of what the file looks like after we are done calclulating the codes MD5 Hashes for files in this folderC:JavaProgsandy825.zip->80A1F295BD9A23407DB08A78A3F719B0C:JavaProgsChannel.java->B12F331B4902CD3B83FCE755C2CF2041C:JavaProgsConstants.java->160EC407312972D51A180BDA76A13D40C:JavaProgsConsumerNode.java->2450D2BC088DFD8C8F6EF0DFB0D91BF3C:JavaProgsFirstSample.class->E9ECF3451F9F5B668E06FA6829F84749C:JavaProgsFirstSample.java->A3BD9339F9B0C2B12D6ED2DDA70EC529C:JavaProgsIndus_Specs.zip->3761280B2BEE66B2D1932F648B6C7B2CC:JavaProgsMessage.java->3A386D68AFAC91BAE8F38685D28396F5C:JavaProgsNetwork.java->8EEA8DD074E0A8BA3CBA4100E3FF52C5C:JavaProgsNetworkEvent.java->8E141C4019354EBCE6BC810A571B606BC:JavaProgsNetworkObject.java->939A41B0B24A5FE04F0116FA285EB322C:JavaProgsNetworkSimulator.zip->D02DFD85556DD8062099CD7C00F5E5A3C:JavaProgsNode.java->41EBE28391A46E7790C97E309864F975C:JavaProgsNoEventsException.java->5D091BBED7CEDF28EA02ED0B6A752CC2C:JavaProgsPosition.java->AEC9DF0CD2E5D771D31AFEF73E7F9D22C:JavaProgsProducerNode.java->8BC8ABBDED9024A5E50E4401297A8642C:JavaProgsReport.zip->4ECF6583529CC8A498A96BBBC9D99687C:JavaProgstext_docs.rar->08BA2159CCE9384A6828695E7A8B0B5DC:JavaProgsworkspace.zip->E188961939F5657D54C328570CA18695C:JavaProgsCVSConflicts->D41D8CD98F00B204E9800998ECF8427EC:JavaProgsCVSEntries->62F0A5D97E61D4474301E563F0FC1571C:JavaProgsCVSRepository->DD97D872EECA6191FB113C3A31D6E1FCC:JavaProgsCVSRoot->2CD13A2C83AFBB419D9CCA55E4EDDF08C:JavaProgsInitializerController.java->2059EBBD8F2737865CB1A3AE71558C04C:JavaProgsInitializerDriver.java->8727BC49022E1D51A89F69113A56D082C:JavaProgsInitializerPathInfo.java->C5FA0E475FAD9B2BC9A81D051DEAF0BBC:JavaProgsInitializerPathNode.java->3B38716462F1212D7B592B7A07387024C:JavaProgsInitializerSearcher.java->F32E51FD22BBD5B044F8F830F3DA3501C:JavaProgsInitializerCVSConflicts->D41D8CD98F00B204E9800998ECF8427EC:JavaProgsInitializerCVSEntries->1EFAD21B1A85F47A372C52963C5342A3C:JavaProgsInitializerCVSRepository->7B0A229EFAA5AD53B4B4FE462DFC9165C:JavaProgsInitializerCVSRoot->2CD13A2C83AFBB419D9CCA55E4EDDF08 Fun Excercises to try(you dont need to do it , but makes the app look cool):1-> Disable the buttons on the form while it is calculating the MD5 hashes2-> Add a progress bar, to indicate how much progress we made3-> Add a listbox and fill in the calculated hashes into that box.Any luck solving those Excercises folks! Quote Link to comment Share on other sites More sharing options...