Public Declare Function GetFileSize Lib "kernel32" Alias "GetFileSize" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long

解决方案 »

  1.   

    1、FileLen 函数示例
    本示例使用 FileLen 来返回文件的字节长度。示例中假设 TESTFILE 为含有数据的文件。Dim MySize
    MySize = FileLen("TESTFILE")Filelen("TESTFILE")2、LOF 函数示例
    本示例使用 LOF 函数来得知已打开文件的大小。本示例假设 TESTFILE 文件内含文本数据。Dim FileLength
    Open "TESTFILE" For Input As #1   ' 打开文件。
    FileLength = LOF(1)   ' 取得文件长度。
    Close #1   ' 关闭文件。
      

  2.   

    可以使用API函数
    Public Function fun_delfile(filepath As String) As Long
    '判断文件大小
    Dim fs, f, s
        Set fs = CreateObject("Scripting.FileSystemObject")
        If Dir(filepath, vbNormal) <> "" Then
            Set f = fs.getfile(filepath)
            fun_delfile = f.Size
            Set fs = Nothing    End If
        
    End Function
      

  3.   

    除了FILELEN外,用OPEN打开过的文件也可以LOF(文件号)取得大小的。当然,除非是文件有必要用OPEN打开,否则不要用这种方法,因为OPEN不能打开超过内存大小的文件的。
      

  4.   

    Declare Function GetFileSize Lib "kernel32" ( _ 
     ByVal hFile As Long, _ 
     lpFileSizeHigh As Long) As Long
      

  5.   


    Private Const OF_READ = &H0&
    Private Declare Function lOpen Lib "kernel32" Alias "_lopen" (ByVal lpPathName As String, ByVal iReadWrite As Long) As Long
    Private Declare Function lclose Lib "kernel32" Alias "_lclose" (ByVal hFile As Long) As Long
    Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
    Dim lpFSHigh As Long
    Public Sub GetInfoF(FilePath As String)
        Dim Pointer As Long, sizeofthefile As Long
        Pointer = lOpen(FilePath, OF_READ)
        'size of the file
        sizeofthefile = GetFileSize(Pointer, lpFSHigh)
        Label1.Caption = sizeofthefile & " bytes"
        lclose Pointer
    End Sub
    Private Sub command1_Click()
        CommonDialog1.ShowOpen
        GetInfoF CommonDialog1.FileName
    End Sub
    Private Sub Form_Load()
        With CommonDialog1
            .DialogTitle = "Select a file"
            .Filter = "All the files|*.*"
        End With
        Command1.Caption = "Select a file"
    End Sub