VB检查文件的大小和检测文件MD5值的代码如下,根据下面注释和实际应用分析:
1、VB代码检查文件大小代码如下。
/***FileLen函数,返回Long,代表一个文件的长度,单位是字节。
*语法:
*FileLen(pathname)
*必要的pathnam参数是用来指定一个文件名的字符串表达式。
*pathname可以包含目录或文件夹、以及驱动器。
*说明:
当调用FileLen函数时,如果所指定的文件已经打开,则返回的值是这个文件在打开前的大小。
注意:若要取得一个打开文件的长度大小,使用LOF函数。
**/
//实际应用:
If FileLen(rst.Fields("pic1")) > 1 Then
Picture1.Picture = LoadPicture(rst.Fields("pic1") & "")
L1.Caption = Trim(rsttemp.Fields("p1time") & "")
Else
Picture1.Picture = LoadPicture("")
L1.Caption = strNorz
End If
//图片文件的路径保存在数据库的pic1字段
2、VB检测文件的MD5值,代码如下。
Option ExplicitOption Base 0
Public Type MD5_CTX
i(1) As Long
buf(3) As Long
inc(63) As Byte
digest(15) As Byte
End Type
Public Declare Sub MD5Init Lib "Cryptdll.dll" (ByVal pContex As Long)
Public Declare Sub MD5Final Lib "Cryptdll.dll" (ByVal pContex As Long)
Public Declare Sub MD5Update Lib "Cryptdll.dll" (ByVal pContex As Long, ByVal lPtr As Long, ByVal nSize As Long)
Public Function ConvBytesToBinaryString(bytesIn() As Byte) As String
Dim i As Long
Dim nSize As Long
Dim strRet As String
nSize = UBound(bytesIn)
For i = 0 To nSize
strRet = strRet & Right$("0" & Hex(bytesIn(i)), 2)
Next
ConvBytesToBinaryString = strRet
End Function
Public Function GetMD5Hash(bytesIn() As Byte) As Byte()
Dim ctx As MD5_CTX
Dim nSize As Long
nSize = UBound(bytesIn) + 1
MD5Init VarPtr(ctx)
MD5Update ByVal VarPtr(ctx), ByVal VarPtr(bytesIn(0)), nSize
MD5Final VarPtr(ctx)
GetMD5Hash = ctx.digest
End Function
Public Function GetMD5Hash_Bytes(bytesIn() As Byte) As String
GetMD5Hash_Bytes = ConvBytesToBinaryString(GetMD5Hash(bytesIn))
End Function
Public Function GetMD5Hash_String(ByVal strIn As String) As String
GetMD5Hash_String = GetMD5Hash_Bytes(StrConv(strIn, vbFromUnicode))
End Function
Public Function GetMD5Hash_File(ByVal strFile As String) As String
Dim lFile As Long
Dim bytes() As Byte
Dim lSize As Long
lSize = FileLen(strFile)
If (lSize) Then
lFile = FreeFile
ReDim bytes(lSize - 1)
Open strFile For Binary As lFile
Get lFile, , bytes
Close lFile
GetMD5Hash_File = GetMD5Hash_Bytes(bytes)
End If
End Function
首先引入System.IO命名空间(在代码文件的最上方)Imports System.IO
然后在需要用到的地方:
Dim mp3fileinfo As New FileInfo(mp3文件完整路径字符串)
'mp3fileinfo.Length就是文件的大小,以B(字节)为单位,除以1024得到KB,再除以1024得到MB
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)