Thursday, September 18, 2008

WIN 32 API -SET FILE TIME

SetFileTime Function

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

* Windows 95: Supported.
* Windows 98: Supported.
* Windows NT: Requires Windows NT 3.1 or later.
* Windows 2000: Supported.
* Windows CE: Requires Windows CE 1.0 or later.

Description & Usage

SetFileTime sets the creation, last-accessed, and last-modified (last written-to) dates and times associated with a file. All the times are in UTC time (Coordinated Universal Time, a.k.a. Greenwich Mean Time (GMT)), not in the system's local time. The times actually stored on the system may vary slightly from the times passed to the function because file times are not stored with perfect resolution (for example, seconds data may be ignored).
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 opened file to set the times of. The file must have been opened with at least write-level access.
lpCreationTime
The date and time to set as the file's creation time.
lpLastAccessTime
The date and time to set as the file's last access time.
lpLastWriteTime
The date and time to set as the file's last write-to (modification) time.

Example(VB 6.0)

' Set the modification time of C:\MyApp\test.txt to
' the current system date and time. Leave the other times as they
' were before calling the function.
Dim hFile As Long ' handle to the opened file
Dim ctime As FILETIME ' the time of creation
Dim atime As FILETIME ' the time of last access
Dim mtime As FILETIME ' the time of last modification
Dim retval As Long ' return value

' First, open the file C:\MyApp\test.txt for both read-level and
' write-level access, since we need to do both.
hFile = CreateFile("C:\MyApp\test.txt", GENERIC_READ Or GENERIC_WRITE, 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)

' Get the system time (already in UTC) as a FILETIME structure.
GetSystemTimeAsFileTime mtime
' Set the retrieved creation and access times and the new modification
' time as the file's times.
retval = SetFileTime(hFile, ctime, atime, mtime)

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

No comments:

Just Answer


JustAnswer.com