VB.NET or C# - How to (HELP)

Discussion in 'Mixed Languages' started by Muerto, Mar 9, 2012.

  1. Muerto

    Muerto MDL Debugger

    Mar 7, 2012
    1,855
    2,103
    60
    #1 Muerto, Mar 9, 2012
    Last edited: Jan 12, 2021
    ...
     
  2. Muerto

    Muerto MDL Debugger

    Mar 7, 2012
    1,855
    2,103
    60
    #2 Muerto, Mar 10, 2012
    Last edited: Jan 12, 2021
    (OP)
    ...
     
  3. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    6,222
    22,280
    210
    #3 Alphawaves, Mar 12, 2012
    Last edited by a moderator: Apr 20, 2017
    Something a little shorter would be:
    Code:
    Public Function CurrentMD5()
            Dim sb As New StringBuilder()
            Dim Hash As MD5 = MD5.Create()
            Using mf As FileStream = File.OpenRead(Process.GetCurrentProcess().ProcessName.ToString() & ".exe")
                For Each b As Byte In Hash.ComputeHash(mf)
                    sb.Append(b.ToString("x2").ToUpper())
                Next
            End Using
            Return sb.ToString()
        End Function
    Usage:
    Code:
    Try
            Label1.Text = CurrentMD5()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
    ;)
     
  4. Muerto

    Muerto MDL Debugger

    Mar 7, 2012
    1,855
    2,103
    60
    #4 Muerto, Mar 12, 2012
    Last edited: Jan 12, 2021
    (OP)
    ...
     
  5. Alphawaves

    Alphawaves Super Moderator/Developer
    Staff Member

    Aug 11, 2008
    6,222
    22,280
    210
    #5 Alphawaves, Mar 13, 2012
    Last edited by a moderator: Apr 20, 2017
    You could also use AppDomain.CurrentDomain.FriendlyName to return the applications name along with the file extension.
    ie: myapp.exe

    VB.NET
    Code:
    Public Function CurrentMD5()
            Dim sb As New StringBuilder()
            Dim Hash As MD5 = MD5.Create()
            Using mf As FileStream = File.OpenRead(AppDomain.CurrentDomain.FriendlyName)
                For Each b As Byte In Hash.ComputeHash(mf)
                    sb.Append(b.ToString("x2").ToUpper())
                Next
            End Using
            Return sb.ToString()
        End Function
    Code:
    Label1.Text = CurrentMD5()
    
    C#
    Code:
     public static string CurrentMD5()
            {
                StringBuilder sb = new StringBuilder();
                MD5 Hash = MD5.Create();
                using (FileStream mf = File.OpenRead(AppDomain.CurrentDomain.FriendlyName))
                {
                    foreach (byte b in Hash.ComputeHash(mf))
                    {
                        sb.Append(b.ToString("x2").ToUpper());
                    }
                }
                return sb.ToString();
            }
    Code:
    label1.Text = CurrentMD5();