Public Const FILE_ATTRIBUTE_READONLY = &H1
Public Const FILE_ATTRIBUTE_HIDDEN = &H2
Public Const FILE_ATTRIBUTE_SYSTEM = &H4
Public Const FILE_ATTRIBUTE_DIRECTORY = &H10
Public Const FILE_ATTRIBUTE_ARCHIVE = &H20
Public Const FILE_ATTRIBUTE_NORMAL = &H80
Public Const FILE_ATTRIBUTE_TEMPORARY = &H100
Public Const FILE_ATTRIBUTE_COMPRESSED = &H800Type FILETIME
    LowDateTime          As Long
    HighDateTime         As Long
End TypeType WIN32_FIND_DATA
    dwFileAttributes     As Long
    ftCreationTime       As FILETIME
    ftLastAccessTime     As FILETIME
    ftLastWriteTime      As FILETIME
    nFileSizeHigh        As Long
    nFileSizeLow         As Long
    dwReserved0          As Long
    dwReserved1          As Long
    cFileName            As String * 260
    cAlternate           As String * 14
End TypeDeclare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As LongType SYSTEMTIME
    wYear As Integer
    wMonth As Integer
    wDayOfWeek As Integer
    wDay As Integer
    wHour As Integer
    wMinute As Integer
    wSecond As Integer
    wMilliseconds As Integer
End Type'自定义文件信息
Type FileInfoList
    Name As String
    CreateTime As String
    CreateTimeWithOnlyDate As String
    LastModifyTime As String
    LastModifyTimeWithOnlyDate As String
    AccessTime As String
    Attribe As String
    Size As String
End TypePublic Function GetFileInfo(tFileName As String) As FileInfoList
    Dim fTime As SYSTEMTIME  '系统时间
    Dim lFile As FILETIME    '文件时间
    
    Dim FileData As WIN32_FIND_DATA
    
    On Error Resume Next
    
    FileData = Findfile(tFileName)        '给出信息
    GetFileInfo.Name = tFileName          ' 给出文件名
    
    If FileData.nFileSizeHigh = 0 Then    '给出文件大小
       GetFileInfo.Size = FileData.nFileSizeLow & "字节"
    Else
       GetFileInfo.Size = FileData.nFileSizeHigh & "字节"
    End If
    
    Dim Ret As Long
    Dim sTmp As String
    
    '创建时间
    Ret = FileTimeToLocalFileTime(FileData.ftCreationTime, lFile)
    Ret = FileTimeToSystemTime(lFile, fTime)
    sTmp = Format(fTime.wDay & "/" & fTime.wMonth & "/" & fTime.wYear, "Long Date") & " " & Format(fTime.wHour & ":" & fTime.wMinute & ":" & fTime.wSecond, "Long Time")
    GetFileInfo.CreateTime = sTmp
    
    Ret = FileTimeToLocalFileTime(FileData.ftCreationTime, lFile)
    Ret = FileTimeToSystemTime(lFile, fTime)
    sTmp = Format(fTime.wDay & "/" & fTime.wMonth & "/" & fTime.wYear, "Short Date")
    GetFileInfo.CreateTimeWithOnlyDate = sTmp
    
    '最后一次写时间
    Ret = FileTimeToLocalFileTime(FileData.ftLastWriteTime, lFile)
    Ret = FileTimeToSystemTime(lFile, fTime)
    sTmp = Format(fTime.wDay & "/" & fTime.wMonth & "/" & fTime.wYear, "Long Date") & " " & Format(fTime.wHour & ":" & fTime.wMinute & ":" & fTime.wSecond, "Long Time")
    GetFileInfo.LastModifyTime = sTmp
    
    Ret = FileTimeToLocalFileTime(FileData.ftLastWriteTime, lFile)
    Ret = FileTimeToSystemTime(lFile, fTime)
    sTmp = Format(fTime.wDay & "/" & fTime.wMonth & "/" & fTime.wYear, "Short Date")
    GetFileInfo.LastModifyTimeWithOnlyDate = sTmp
    
    '最后一次访问时间
    Ret = FileTimeToLocalFileTime(FileData.ftLastAccessTime, lFile)
    Ret = FileTimeToSystemTime(lFile, fTime)
    sTmp = Format(fTime.wDay & "/" & fTime.wMonth & "/" & fTime.wYear, "Long Date")
    GetFileInfo.AccessTime = sTmp
    
    '给出文件属性
    Dim sAttribe As String
    If (FileData.dwFileAttributes And FILE_ATTRIBUTE_HIDDEN) = FILE_ATTRIBUTE_HIDDEN Then
        sAttribe = "Hide"
    Else
        sAttribe = ""
    End If
    
    If (FileData.dwFileAttributes And FILE_ATTRIBUTE_SYSTEM) = FILE_ATTRIBUTE_SYSTEM Then
       sAttribe = "System"
    Else
       sAttribe = ""
    End If
    
    If (FileData.dwFileAttributes And FILE_ATTRIBUTE_READONLY) = FILE_ATTRIBUTE_READONLY Then
       sAttribe = "ReadOnly"
    Else
       sAttribe = ""
    End If
    
    If (FileData.dwFileAttributes And FILE_ATTRIBUTE_ARCHIVE) = FILE_ATTRIBUTE_ARCHIVE Then
       sAttribe = "Archive"
    Else
       sAttribe = ""
    End If
    
    If (FileData.dwFileAttributes And FILE_ATTRIBUTE_TEMPORARY) = FILE_ATTRIBUTE_TEMPORARY Then
       sAttribe = "Temp"
    Else
       sAttribe = ""
    End If
    
    If (FileData.dwFileAttributes And FILE_ATTRIBUTE_NORMAL) = FILE_ATTRIBUTE_NORMAL Then
       sAttribe = "Normal"
    Else
       sAttribe = ""
    End If
    
    If (FileData.dwFileAttributes And FILE_ATTRIBUTE_COMPRESSED) = FILE_ATTRIBUTE_COMPRESSED Then
       sAttribe = "Compressed"
    Else
       sAttribe = ""
    End If
End FunctionPublic Function Findfile(strFileName As String) As WIN32_FIND_DATA
    Dim Win32Data As WIN32_FIND_DATA
    Dim plngFirstFileHwnd As Long
    Dim plngRtn As Long
    
    On Error Resume Next
    plngFirstFileHwnd = FindFirstFile(strFileName, Win32Data)   ' 给出文件句柄
    
    If plngFirstFileHwnd = 0 Then
      Findfile.cFileName = "错误,文件可能不存在?"
    Else
      Findfile = Win32Data                                      ' 返回结果
    End If
    plngRtn = FindClose(plngFirstFileHwnd)
End Function
-----------------------------------
比如修改日期,结果有时候返回是正确的,有时候是错误的。比如正确的日期为2003年12月1日,它的返回结果是2003年1月12日。而2003年5月27日又能返回正确的结果。这是为什么?

解决方案 »

  1.   

    用FSO好了
        Dim fs, f, s, filespec
        filespec = InputBox("file")
        Set fs = CreateObject("Scripting.FileSystemObject")
        Set f = fs.GetFile(filespec)
        MsgBox f.DateCreated
        MsgBox f.DateLastAccessed
        '.......
        '......
      

  2.   

    给你一端代码参考一下:
    'This program needs a Dialog box, named CDBox1
    '  (To add the Common Dialog Box to your tools menu, go to Project->Components (or press CTRL-T)
    '   and select Microsoft Common Dialog control)
    Private Type FILETIME
        dwLowDateTime As Long
        dwHighDateTime As Long
    End Type
    Private Type SHFILEOPSTRUCT
        hWnd As Long
        wFunc As Long
        pFrom As String
        pTo As String
        fFlags As Integer
        fAborted As Boolean
        hNameMaps As Long
        sProgress As String
    End Type
    Private Type SYSTEMTIME
        wYear As Integer
        wMonth As Integer
        wDayOfWeek As Integer
        wDay As Integer
        wHour As Integer
        wMinute As Integer
        wSecond As Integer
        wMilliseconds As Integer
    End Type
    Private Const GENERIC_WRITE = &H40000000
    Private Const OPEN_EXISTING = 3
    Private Const FILE_SHARE_READ = &H1
    Private Const FILE_SHARE_WRITE = &H2
    Private Const FO_DELETE = &H3
    Private Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long
    Private Declare Function CreateDirectory Lib "kernel32" Alias "CreateDirectoryA" (ByVal lpPathName As String, lpSecurityAttributes As Long) As Long
    Private Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" (ByVal lpFileName As String) As Long
    Private Declare Function GetFileSize Lib "kernel32" (ByVal hFile As Long, lpFileSizeHigh As Long) As Long
    Private Declare Function GetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
    Private Declare Function MoveFile Lib "kernel32" Alias "MoveFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String) As Long
    Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Long, 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 Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
    Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long
    Private Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As FILETIME, lpLocalFileTime As FILETIME) As Long
    Private Sub Form_Load()
        
        Dim lngHandle As Long, SHDirOp As SHFILEOPSTRUCT, lngLong As Long
        Dim Ft1 As FILETIME, Ft2 As FILETIME, SysTime As SYSTEMTIME
        'Set the dialog's title
        CDBox.DialogTitle = "Choose a file ..."
        'Raise an error when the user pressed cancel
        CDBox.CancelError = True
        'Show the 'Open File'-dialog
        CDBox.ShowOpen
        'Create a new directory
        CreateDirectory "C:\KPD-Team", ByVal &H0
        'Copy the selected file to our new directory
        CopyFile CDBox.filename, "C:\KPD-Team\" + CDBox.FileTitle, 0
        'Rename the file
        MoveFile "C:\KPD-Team\" + CDBox.FileTitle, "C:\KPD-Team\test.kpd"
        'Open the file
        lngHandle = CreateFile("C:\KPD-Team\test.kpd", GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, 0, 0)
        'Get the file's size
        MsgBox "The size of the selected file is" + Str$(GetFileSize(lngHandle, lngLong)) + " bytes."
        'Get the fil's time
        GetFileTime lngHandle, Ft1, Ft1, Ft2
        'Convert the file time to the local file time
        FileTimeToLocalFileTime Ft2, Ft1
        'Convert the file time to system file time
        FileTimeToSystemTime Ft1, SysTime
        MsgBox "The selected file was created on" + Str$(SysTime.wMonth) + "/" + Ltrim(Str$(SysTime.wDay)) + "/" + Ltrim(Str$(SysTime.wYear))
        'Close the file
        CloseHandle lngHandle
        'Delete the file
        DeleteFile "C:\KPD-Team\test.kpd"
        With SHDirOp
            .wFunc = FO_DELETE
            .pFrom = "C:\KPD-Team"
        End With
        'Delete the directory
        SHFileOperation SHDirOp
        End
    End Sub