Imports System.IO
Imports System.Security.Cryptography
Public Class FileEx
'''
''' Reads the file at `FilePath` and computes a SHA256 Hash from its contents
'''
'''
'''
Public Shared Function GetChecksumFromFileContents(pFilePath As String) As String
Try
Using oFileStream = IO.File.OpenRead(pFilePath)
Using oStream As New BufferedStream(oFileStream, 1200000)
Dim oChecksum() As Byte = SHA256.Create.ComputeHash(oStream)
Return BaseUtils.FormatHash(oChecksum)
End Using
End Using
Catch ex As Exception
Return Nothing
End Try
End Function
Public Shared Function GetHashFromFileContents(pFilePath As String) As String
Return GetChecksumFromFileContents(pFilePath)
End Function
'''
''' Returns a Dictionary path in the form of [Base]\[Year]\[Month]\[Day]
'''
''' The basedirectory
''' The date to be used
''' The final directory path
Public Shared Function GetDateDirectory(pBaseDirectory As String, pDate As Date) As String
Dim oDateDirectory = StringEx.GetDateString(pDate)
Dim oFinalDirectory As String = Path.Combine(pBaseDirectory, oDateDirectory)
Return oFinalDirectory
End Function
'''
''' Returns a Dictionary path in the form of [Base]\[Year]\[Month]\[Day] based on the current date
'''
''' The basedirectory
''' The final directory path
Public Shared Function GetDateDirectory(pBaseDirectory As String) As String
Return GetDateDirectory(pBaseDirectory, Now)
End Function
'''
''' Creates a Dictionary in the form of [Base]\[Year]\[Month]\[Day]
'''
''' The basedirectory
''' The date to be used
''' The created path. If the directory cannot be created, Nothing.
Public Shared Function CreateDateDirectory(pBaseDirectory As String, pDate As Date) As String
Dim oDateDirectory = StringEx.GetDateString(pDate)
Dim oFinalDirectory As String = Path.Combine(pBaseDirectory, oDateDirectory)
If Directory.Exists(oFinalDirectory) = False Then
Try
Directory.CreateDirectory(oFinalDirectory)
Catch ex As Exception
Return Nothing
End Try
End If
Return oFinalDirectory
End Function
'''
''' Creates a Dictionary in the form of [Base]\[Year]\[Month]\[Day] based on the current date
'''
''' The basedirectory
''' The created path. If the directory cannot be created, Nothing.
Public Shared Function CreateDateDirectory(pBaseDirectory As String) As String
Return CreateDateDirectory(pBaseDirectory, Now)
End Function
End Class