Option Explicit
Private Declare Function GetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
Private Const OF_CANCEL = &H800
Private Const OF_CREATE = &H1000
Private Const OF_DELETE = &H200
Private Const OF_EXIST = &H4000
Private Const OF_PARSE = &H100
Private Const OF_PROMPT = &H2000
Private Const OF_READ = &H0
Private Const OF_READWRITE = &H2
Private Const OF_REOPEN = &H8000
Private Const OF_SHARE_COMPAT = &H0
Private Const OF_SHARE_DENY_NONE = &H40
Private Const OF_SHARE_DENY_READ = &H30
Private Const OF_SHARE_DENY_WRITE = &H20
Private Const OF_SHARE_EXCLUSIVE = &H10
Private Const OF_VERIFY = &H400
Private Const OF_WRITE = &H1
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 Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Sub Command1_Click()
Dim fStruct As OFSTRUCT
Dim CreationTime As FILETIME
Dim LastAccessTime As FILETIME
Dim LastWriteTime As FILETIME
Dim hFile As Long
With fStruct
.cBytes = 136
End With
hFile = OpenFile("c:\123.txt", fStruct, OF_READ)
GetFileTime hFile, CreationTime, LastAccessTime, LastWriteTime
Debug.Print CreationTime.dwHighDateTime
Debug.Print CreationTime.dwLowDateTime
End Sub
但dwHighDateTime和dwLowDateTime不是我们一眼能看懂的,得转换。
MSDN是这样说的:
FILETIME
The FILETIME structure is a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601.
Members
dwLowDateTime
Specifies the low-order 32 bits of the file time.
dwHighDateTime
Specifies the high-order 32 bits of the file time.
Remarks
It is not recommended that you add and subtract values from the FILETIME structure to obtain relative times. Instead, you should
Copy the resulting FILETIME structure to a LARGE_INTEGER structure.
Use normal 64-bit arithmetic on the LARGE_INTEGER value.
类型定义
Type FILETIME ' 8 Bytes
dwLowDateTime As Long
dwHighDateTime As Long
End Type
说明
windows提供了一种特殊的机制,可以记录文件的访问及创建时间。在win32环境中,这些信息以64位值的形式保存,量度的是自1601年1月1日以来经历的100ns时间单位数量(64-bit number specifying the elapsed time since January 1, 1601, in 100-nanosecond increments.)
字段表
字段 类型及说明
dwLowDateTime Long,Low and high-order 32 bits of the file time
dwHighDateTime
注解
文件时间在系统中通常用“协同世界时间”(UTC)的格式保存,但同时提供了在UTC及本地时间之间转换的函数。FILETIME结构里可包含UTC或本地时间——由我们自行决定在结构中包含什么时间
VB声明
Declare Function GetFileTime Lib "kernel32" Alias "GetFileTime" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
说明
取得指定文件的时间信息
返回值
Long,非零表示成功,零表示失败。会设置GetLastError
参数表
参数 类型及说明
hFile Long,文件的句柄
lpCreationTime FILETIME,用于装载文件的创建时间
lpLastAccessTime FILETIME,用于装载文件上一次访问的时间(FAT文件系统不支持这一特性)
lpLastWriteTime FILETIME,用于装载文件上一次修改的时间
注解
如果不需要特定的信息,那么lpCreationTime,lpLastAccessTime,lpLastWriteTime都可以设置为零(用ByVal As Long)。这个函数返回的文件时间采用UTC格式
Private Declare Function GetFileTime Lib "kernel32" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type