新建From1(窗体),代码:
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 = App.Path & "\manongku.txt"
FileHandle = OpenFile(strFileName, leRpOpenBuff, OF_READ)
Dim lngFileSize As Long
Dim temp As Long
temp = GetFileSize(FileHandle, lngFileSize)
MsgBox f_FormatKB(temp), vbInformation, "File Size in String"
End Sub
Public Function f_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
f_FormatKB = Left$(Result, InStr(Result, vbNullChar) - 1)
End If
End Function
在当前目录放一个manongku.txt然后运行程序。