GetFileTime Function

Declare Function GetFileTime Lib "kernel32.dll" (ByVal hFile As Long, lpCreationTime As FILETIME, lpLastAccessTime As FILETIME, lpLastWriteTime As FILETIME) As Long

Platforms

Description & Usage

GetFileTime determines the times and dates of creation, last access, and last modification (write-to) of a file. Each of these times are placed in the corresponding structures passed to the function. All times obtained by the function are in UTC time (Coordinated Universal Time, a.k.a. Greenwich Mean Time (GMT)), not in the system's local time. Note that, depending on the operating system, the exact resolution of file times may vary -- the file times obtained by the function may not correspond to the actual date and time of creation/access/modification simply because the operating system does not store the information that precisely.

Return Value

If an error occured, the function returns 0 (use GetLastError to get the error code). If successful, the function returns a non-zero value.

Visual Basic-Specific Issues

None.

Parameters

hFile
A handle to the file to obtain the creation, last access, and last modification times and dates of. The file must have been opened with at least read-level access.
lpCreationTime
Receives the time and date of when the file was created.
lpLastAccessTime
Receives the time and date of when the file was last accessed.
lpLastWriteTime
Receives the time and date of when the file was last written to or modified.

Example

' This code is licensed according to the terms and conditions listed here.

' Display the date on which the file C:\MyApp\test.txt was
' created.  Note how the time zone conversion is necessary.
Dim hFile As Long  ' handle to the opened file
Dim ctime As FILETIME  ' receives time of creation
Dim atime As FILETIME  ' receives time of last access
Dim mtime As FILETIME  ' receives time of last modification
Dim thetime As SYSTEMTIME  ' used to manipulate the time
Dim retval As Long  ' return value

' First, open the file C:\MyApp\test.txt for read-level access.  Note the
' expression necessary to pass 0 as lpSecurityAttributes.
hFile = CreateFile("C:\MyApp\test.txt", GENERIC_READ, FILE_SHARE_READ, ByVal CLng(0), OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, 0)
If hFile = -1 Then
  Debug.Print "Could not open the file successfully -- aborting."
  End  ' terminate the program
End If

' Next, get the creation, last-access, and last-modification times.
retval = GetFileTime(hFile, ctime, atime, mtime)
' Convert the creation time to the local time zone.
retval = FileTimeToLocalFileTime(ctime, ctime)
' Convert the FILETIME format to the SYSTEMTIME format.
retval = FileTimeToSystemTime(ctime, thetime)

' Display the date of creation of the file to the user.
Debug.Print "The file was created on "; thetime.wMonth; "-"; thetime.wDay; "-"; thetime.wYear

' Close the file to free up resources.
retval = CloseHandle(hFile)

See Also

GetFileInformationByHandle, SetFileTime

Category

Files

Go back to the alphabetical Function listing.
Go back to the Reference section index.


Last Modified: October 1, 1999
This page is copyright © 1999 Paul Kuliniewicz. Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/g/getfiletime.html