用户登录  |  用户注册
首 页商业源码原创产品编程论坛
当前位置:PB创新网文章中心编程技巧VisualBasic

将文件大小变成相应的字符串

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2009-03-16 19:50:08

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

Tags:

作者:佚名

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
PB创新网ourmis.com】Copyright © 2000-2009 . All Rights Reserved .
页面执行时间:28,000.00000 毫秒
Email:ourmis@126.com QQ:2322888 蜀ICP备05006790号