Skip to content

Commit

Permalink
Catch exceptions while attempting to compute hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Strappazzon committed Mar 12, 2020
1 parent 4a9c941 commit ba201b1
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions src/Form1.vb
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,32 @@ Public Class Form1
End Sub

Private Sub HashInput_DoWork(sender As Object, e As DoWorkEventArgs) Handles HashInput.DoWork
'Check whether the specified/dropped path is a folder or a file
'//stackoverflow.com/a/439478
Dim IsDir As Boolean = (File.GetAttributes(PathTextBox.Text) And FileAttributes.Directory) = FileAttributes.Directory

If IsDir = True Then
'Hash the selected folder
Dim Hash As String = Hasher.HashDirectory(PathTextBox.Text)
e.Result = Hash
Else
'Hash the selected file
Dim Hash As String = Hasher.HashFile(PathTextBox.Text)
e.Result = Hash
End If
Try
'Check whether the specified/dropped path is a folder or a file
'//stackoverflow.com/a/439478
Dim IsDir As Boolean = (File.GetAttributes(PathTextBox.Text) And FileAttributes.Directory) = FileAttributes.Directory

If IsDir = True Then
'Hash the selected folder
Dim Hash As String = Hasher.HashDirectory(PathTextBox.Text)
e.Result = Hash
Else
'Hash the selected file
Dim Hash As String = Hasher.HashFile(PathTextBox.Text)
e.Result = Hash
End If
Catch ex As Exception
e.Result = "Error"
MessageBox.Show("An error occurred while attempting to compute the hash: " & ex.Message(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

Private Sub HashInput_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles HashInput.RunWorkerCompleted
'Write the computed hash
HashTextBox.Text = e.Result()

'Write the hash to a text file
If CopyToHashTxtCheckBox.Checked = True Then
If e.Result <> "Error" AndAlso CopyToHashTxtCheckBox.Checked = True Then
Using S As New SaveFileDialog
S.Title = "Save hash as..."
S.InitialDirectory = PathTextBox.Text
Expand All @@ -73,7 +78,7 @@ Public Class Form1
End If

'Copy the hash to clipboard
If CopyToClipboardCheckBox.Checked = True Then
If e.Result <> "Error" AndAlso CopyToClipboardCheckBox.Checked = True Then
Clipboard.SetText(e.Result())
End If
End Sub
Expand Down

0 comments on commit ba201b1

Please sign in to comment.