Jump to content
zbeng

VisualBasic 2005

Recommended Posts

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 extending

We 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 differently

Nothing much really. The basic logic is along the lines "What do we need to do?"

The answer to that question is simple

a. Find a way to browse through a directory and collect all the filenames

b. Calculate the hashes for each file

c. 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 create

Ok! here is the screenshot:

hashcalcrz7.jpg

Full code needed by the application to run:

Imports System.IO
Imports System
Imports System.Security.Cryptography
Imports System.Text
Imports System.Collections.ObjectModel

Public 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 Sub


Here is a copy of what the file looks like after we are done calclulating the codes

MD5 Hashes for files in this folder
C:JavaProgsandy825.zip->80A1F295BD9A23407DB08A78A3F719B0
C:JavaProgsChannel.java->B12F331B4902CD3B83FCE755C2CF2041
C:JavaProgsConstants.java->160EC407312972D51A180BDA76A13D40
C:JavaProgsConsumerNode.java->2450D2BC088DFD8C8F6EF0DFB0D91BF3
C:JavaProgsFirstSample.class->E9ECF3451F9F5B668E06FA6829F84749
C:JavaProgsFirstSample.java->A3BD9339F9B0C2B12D6ED2DDA70EC529
C:JavaProgsIndus_Specs.zip->3761280B2BEE66B2D1932F648B6C7B2C
C:JavaProgsMessage.java->3A386D68AFAC91BAE8F38685D28396F5
C:JavaProgsNetwork.java->8EEA8DD074E0A8BA3CBA4100E3FF52C5
C:JavaProgsNetworkEvent.java->8E141C4019354EBCE6BC810A571B606B
C:JavaProgsNetworkObject.java->939A41B0B24A5FE04F0116FA285EB322
C:JavaProgsNetworkSimulator.zip->D02DFD85556DD8062099CD7C00F5E5A3
C:JavaProgsNode.java->41EBE28391A46E7790C97E309864F975
C:JavaProgsNoEventsException.java->5D091BBED7CEDF28EA02ED0B6A752CC2
C:JavaProgsPosition.java->AEC9DF0CD2E5D771D31AFEF73E7F9D22
C:JavaProgsProducerNode.java->8BC8ABBDED9024A5E50E4401297A8642
C:JavaProgsReport.zip->4ECF6583529CC8A498A96BBBC9D99687
C:JavaProgstext_docs.rar->08BA2159CCE9384A6828695E7A8B0B5D
C:JavaProgsworkspace.zip->E188961939F5657D54C328570CA18695
C:JavaProgsCVSConflicts->D41D8CD98F00B204E9800998ECF8427E
C:JavaProgsCVSEntries->62F0A5D97E61D4474301E563F0FC1571
C:JavaProgsCVSRepository->DD97D872EECA6191FB113C3A31D6E1FC
C:JavaProgsCVSRoot->2CD13A2C83AFBB419D9CCA55E4EDDF08
C:JavaProgsInitializerController.java->2059EBBD8F2737865CB1A3AE71558C04
C:JavaProgsInitializerDriver.java->8727BC49022E1D51A89F69113A56D082
C:JavaProgsInitializerPathInfo.java->C5FA0E475FAD9B2BC9A81D051DEAF0BB
C:JavaProgsInitializerPathNode.java->3B38716462F1212D7B592B7A07387024
C:JavaProgsInitializerSearcher.java->F32E51FD22BBD5B044F8F830F3DA3501
C:JavaProgsInitializerCVSConflicts->D41D8CD98F00B204E9800998ECF8427E
C:JavaProgsInitializerCVSEntries->1EFAD21B1A85F47A372C52963C5342A3
C:JavaProgsInitializerCVSRepository->7B0A229EFAA5AD53B4B4FE462DFC9165
C: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 hashes

2-> Add a progress bar, to indicate how much progress we made

3-> Add a listbox and fill in the calculated hashes into that box.

Any luck solving those Excercises folks!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...