将文件大小变成相应的字符串
Shlwapi.dll是一个跟随IE一起发行的文件。利用该文件中的一个API函数,你就可以将以字节显示的文件的大小转变成相应的字符串。例如:"1.41 KB" 或 "1.32 MB." 在NT4, Windows 95+IE, 以及 Windows 98 这几个操作系统中都能找到Shlwapi.dll文件。
下面是完整的源程序:
Option Explicit
Private Declare Function StrFormatByteSize Lib "shlwapi" Alias "StrFormatByteSizeA" (ByVal dw As Long, ByVal pszBuf As String, ByRef cchBuf As Long) As String
Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
Private Const OFS_MAXPATHNAME = 128
Private Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName(OFS_MAXPATHNAME) As Byte
End Type
Private Const OF_READ = &H0
Private Sub Form_Load()
Dim FileHandle As Long
Dim strFileName As String
Dim leRpOpenBuff As OFSTRUCT
strFileName = "c:\autoexec.bat"
FileHandle = OpenFile(strFileName, leRpOpenBuff, OF_READ)
Dim lngFileSize As Long
Dim temp As Long
temp = GetFileSize(FileHandle, lngFileSize)
MsgBox FormatKB(temp), vbInformation, "File Size in String"
End Sub
Public Function FormatKB(ByVal Amount As Long) As String
Dim Buffer As String
Dim Result As String
Buffer = Space$(255)
Result = StrFormatByteSize(Amount, Buffer, Len(Buffer))
If InStr(Result, vbNullChar) > 1 Then
FormatKB = Left$(Result, InStr(Result, vbNullChar) - 1)
End If
End Function