Visual Basic Example

This was a proof of concept for getting the MD5 for a file along with a few other file information details. Most of the code was taken from VB Dot NET Forums.

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

Public Class Form1

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

  End Sub

  Private Sub btnGetInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetInfo.Click
    OpenFileDialog1.ShowDialog()

    Dim strFile As String = OpenFileDialog1.FileName.ToString
    Dim md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
    Dim f As FileStream = New FileStream(strFile, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
    Dim finfo As New FileInfo(strFile)

    md5.ComputeHash(f)
    f.Close()

    Dim hash As Byte() = md5.Hash
    Dim buff As StringBuilder = New StringBuilder
    Dim hashByte As Byte
    For Each hashByte In hash
      buff.Append(String.Format("{0:X2}", hashByte))
    Next

    lstResults.Items.Add("Filename: " & finfo.Name)
    lstResults.Items.Add("Size    : " & finfo.Length)
    lstResults.Items.Add("MD5     : " & buff.ToString())
  End Sub
End Class

System.Security.Cryptography
System.IO
System.Text

page_revision: 4, last_edited: 1204409616|%e %b %Y, %H:%M %Z (%O ago)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License