Private Const OFS_MAXPATHNAME = 128
Private Const OF_READ = &H0
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 Type FileTime
    dwLowDateTime As Long
    dwHighDateTime As Long
End Type
Private Type BY_HANDLE_FILE_INFORMATION
    dwFileAttributes As Long
    ftCreationTime As FileTime
    ftLastAccessTime As FileTime
    ftLastWriteTime As FileTime
    dwVolumeSerialNumber As Long
    nFileSizeHigh As Long
    nFileSizeLow As Long
    nNumberOfLinks As Long
    nFileIndexHigh As Long
    nFileIndexLow As Long
End Type
Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
Private Declare Function GetFileInformationByHandle Lib "kernel32" (ByVal hFile As Long, lpFileInformation As BY_HANDLE_FILE_INFORMATION) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)Private Sub Command1_Click()
    Dim FileHandle As Long
    Dim FileInfo As BY_HANDLE_FILE_INFORMATION
    Dim lpReOpenBuff As OFSTRUCT
    
    FileHandle = OpenFile("d:\office1.cab", lpReOpenBuff, OF_READ)
    Call GetFileInformationByHandle(FileHandle, FileInfo)
    Call CloseHandle(FileHandle)
    
    Dim c As Currency
    CopyMemory ByVal VarPtr(c), ByVal VarPtr(FileInfo.nFileSizeLow), ByVal 4&
    CopyMemory ByVal VarPtr(c) + 4, ByVal VarPtr(FileInfo.nFileIndexHigh), ByVal 4&
    MsgBox FormatNumber(c * 10000, 0, , , vbTrue) & "(bytes)"
End Sub

解决方案 »

  1.   

    Private Const OPEN_EXISTING = 3
    Private Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
    End Type
    Private Type BY_HANDLE_FILE_INFORMATION
        dwFileAttributes As Long
        ftCreationTime As FILETIME
        ftLastAccessTime As FILETIME
        ftLastWriteTime As FILETIME
        dwVolumeSerialNumber As Long
        nFileSizeHigh As Long
        nFileSizeLow As Long
        nNumberOfLinks As Long
        nFileIndexHigh As Long
        nFileIndexLow As Long
    End Type
    Private Declare Function GetFileInformationByHandle Lib "kernel32" (ByVal hFile As Long, lpFileInformation As BY_HANDLE_FILE_INFORMATION) As Long
    Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Sub Form_Load()
        Dim hFile As Long, FileInfo As BY_HANDLE_FILE_INFORMATION
        'create a handle to the file 'c:\autoexec.bat'
        hFile = CreateFile("c:\autoexec.bat", 0, 0, ByVal 0&, OPEN_EXISTING, 0, ByVal 0&)
        'retrieve the file information
        GetFileInformationByHandle hFile, FileInfo
        'close the handle
        CloseHandle hFile
        'show the result
        MsgBox "File size: " + CStr(FileInfo.nFileSizeLow), vbInformation
    End SubConst GENERIC_READ = &H80000000
    Const FILE_SHARE_READ = &H1
    Const OPEN_EXISTING = 3
    Const FILE_TYPE_CHAR = &H2
    Const FILE_TYPE_DISK = &H1
    Const FILE_TYPE_PIPE = &H3
    Const FILE_TYPE_UNKNOWN = &H0
    Private Declare Function GetFileType Lib "kernel32" (ByVal hFile As Long) As Long
    Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
    Private Declare Function GetFileSizeEx Lib "kernel32" (ByVal hFile As Long, lpFileSize As Currency) As Boolean
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Sub Form_Paint()
        Dim hFile As Long, nSize As Currency, sSave As String
        'open the file
        hFile = CreateFile("c:\autoexec.bat", GENERIC_READ, FILE_SHARE_READ, ByVal 0&, OPEN_EXISTING, ByVal 0&, ByVal 0&)
        'get the filesize
        GetFileSizeEx hFile, nSize
        'retrieve the file type
        Select Case GetFileType(hFile)
            Case FILE_TYPE_UNKNOWN
                sSave = "The type of the specified file is unknown"
            Case FILE_TYPE_DISK
                sSave = "The specified file is a disk file"
            Case FILE_TYPE_CHAR
                sSave = "The specified file is a character file, typically an LPT device or a console"
            Case FILE_TYPE_PIPE
                sSave = "The specified file is either a named or anonymous pipe"
        End Select
        'close the file
        CloseHandle hFile
        MsgBox "File Type: " + sSave + vbCrLf + "Size:" + Str$(nSize * 10000) + " bytes"
    End Sub
     
    vb.netImports System
    Imports System.IO
    Public Module modmain
       Sub Main()
           Dim MyFile as new FileInfo ("c:\autoexec.bat")
          Console.WriteLine("The length of autoexec.bat is " _
             + MyFile.Length.ToString + " bytes.")
       End Sub
    End Module
      

  2.   

    非常感谢!请再去http://www.csdn.net/Expert/TopicView1.asp?id=1047780领分。