[AYUDA] VB.NET BUSCAR EN UN DIRECTORIO Y COMPARAR EL CHEKSUM MD5

Iniciado por DragonsWP, 20 Julio 2013, 00:23 AM

0 Miembros y 1 Visitante están viendo este tema.

DragonsWP

Buenas tardes, necesito ayuda necesito buscar en un directorio (Cantidad de Archivos en carpeta 500) y comparar el cheksum md5 de un archivo, con el que le indique.
estoy usando vb.net 2008 aver si alguien me puede ayudar con este asunto.

Saludos.

Atte.

Brayan Quispe

Eleкtro

Código (vbnet) [Seleccionar]
#Region " Get Files "

    ' [ Get Files Function ]
    '
    ' // By Elektro H@cker
    '
    ' Examples :
    '
    ' For Each file In Get_Files("C:\Windows", False) : MsgBox(file.Name) : Next
    '
    ' For Each file In Get_Files("C:\Windows", True, "dll")   : MsgBox(file.Name) : Next
    ' For Each file In Get_Files("C:\Windows", True, ".dll")  : MsgBox(file.Name) : Next
    ' For Each file In Get_Files("C:\Windows", True, "*.dll") : MsgBox(file.Name) : Next
    '
    ' For Each file In Get_Files("C:\Windows", False, {"dll", "ini"})     : MsgBox(file.Name) : Next
    ' For Each file In Get_Files("C:\Windows", False, {".dll", ".ini"})   : MsgBox(file.Name) : Next
    ' For Each file In Get_Files("C:\Windows", False, {"*.dll", "*.ini"}) : MsgBox(file.Name) : Next

    ' Get Files {directory} {recursive}
    Private Function Get_Files(ByVal directory As String, ByVal recursive As Boolean) As List(Of IO.FileInfo)
        Dim searchOpt As IO.SearchOption = If(recursive, IO.SearchOption.AllDirectories, IO.SearchOption.TopDirectoryOnly)
        Return IO.Directory.GetFiles(directory, "*", searchOpt).Select(Function(p) New IO.FileInfo(p)).ToList
    End Function

    ' Get Files {directory} {recursive} {ext}
    Private Function Get_Files(ByVal directory As String, ByVal recursive As Boolean, ext As String) As List(Of IO.FileInfo)

        If ext.StartsWith("*") Then
            ext = ext.Substring(1, ext.Length - 1)
        ElseIf Not ext = "*" AndAlso Not ext.StartsWith(".") Then
            ext = ("." & ext)
        ElseIf ext = "*" Then
            ext = Nothing
        End If

        Dim searchOpt As IO.SearchOption = If(recursive, IO.SearchOption.AllDirectories, IO.SearchOption.TopDirectoryOnly)
        Return IO.Directory.GetFiles(directory, "*" & ext, searchOpt).Select(Function(p) New IO.FileInfo(p)).ToList

    End Function

    ' Get Files {directory} {recursive} {exts()}
    Private Function Get_Files(ByVal directory As String, ByVal recursive As Boolean, ParamArray exts() As String) As List(Of IO.FileInfo)

        Dim FileExts(exts.Count) As String
        Dim ExtCount As Int32 = 0

        For Each ext In exts
            If ext.StartsWith("*") Then
                FileExts(ExtCount) = ext.Substring(1, ext.Length - 1)
            ElseIf Not ext = "*" AndAlso Not ext.StartsWith(".") Then
                FileExts(ExtCount) = ("." & ext)
            ElseIf Not ext = "*" AndAlso ext.StartsWith(".") Then
                FileExts(ExtCount) = ext
            ElseIf ext = "*" Then
                FileExts(ExtCount) = Nothing
            End If
            ExtCount += 1
        Next

        Dim searchOpt As IO.SearchOption = If(recursive, IO.SearchOption.AllDirectories, IO.SearchOption.TopDirectoryOnly)
        Dim filenameExtComparer As New FilenameExtensionComparer
        Return IO.Directory.GetFiles(directory, "*", searchOpt).Where(Function(o) FileExts.Contains(IO.Path.GetExtension(o), filenameExtComparer)).Select(Function(p) New IO.FileInfo(p)).ToList

    End Function

    ' FilenameExtensionComparer (Ignore Case) needed for "Get Files {directory} {recursive} {exts()}" overload.
    Public Class FilenameExtensionComparer : Implements IEqualityComparer(Of String)

        Public Function Equals1(s As String, t As String) As Boolean Implements IEqualityComparer(Of String).Equals
            Return String.Compare(s, t, StringComparison.OrdinalIgnoreCase) = 0
        End Function

        Public Function GetHashCode1(s As String) As Integer Implements IEqualityComparer(Of String).GetHashCode
            Return s.GetHashCode()
        End Function

    End Class

#End Region



Código (vbnet) [Seleccionar]
        #Region " Get MD5 Of File Function "
     
          ' [ Get MD5 Of File Function ]
          '
          ' Examples :
          '
          ' MsgBox(Get_MD5_Of_File("C:\Test.txt"))
     
          Private Function Get_MD5_Of_File(ByVal File As String) As String
              Using MD5_Reader As New System.IO.FileStream(File, IO.FileMode.Open, IO.FileAccess.Read)
                  Using MD5 As New System.Security.Cryptography.MD5CryptoServiceProvider
                      Dim MD5_Byte() As Byte = MD5.ComputeHash(MD5_Reader)
                      Dim MD5_Hex As New System.Text.StringBuilder(MD5.ComputeHash(MD5_Reader).Length * 2)
                      For Number As Integer = 0 To MD5_Byte.Length - 1
                          Application.DoEvents()
                          MD5_Hex.Append(MD5_Byte(Number).ToString("X2"))
                      Next
                      Return MD5_Hex.ToString().ToLower
                  End Using
              End Using
          End Function
     
       #End Region


...Aquí más -> Librería de Snippets !! (Posteen aquí sus snippets)