3 out of 4 rated this helpful - Rate this topic

SetFileTime function

Applies to: desktop apps only

Sets the date and time that the specified file or directory was created, last accessed, or last modified.

Syntax

BOOL WINAPI SetFileTime(
  __in      HANDLE hFile,
  __in_opt  const FILETIME *lpCreationTime,
  __in_opt  const FILETIME *lpLastAccessTime,
  __in_opt  const FILETIME *lpLastWriteTime
);

Parameters

hFile [in]

A handle to the file or directory. The handle must have been created using the CreateFile function with the FILE_WRITE_ATTRIBUTES access right. For more information, see File Security and Access Rights.

lpCreationTime [in, optional]

A pointer to a FILETIME structure that contains the new creation date and time for the file or directory. This parameter can be NULL if the application does not need to change this information.

lpLastAccessTime [in, optional]

A pointer to a FILETIME structure that contains the new last access date and time for the file or directory. The last access time includes the last time the file or directory was written to, read from, or (in the case of executable files) run. This parameter can be NULL if the application does not need to change this information.

To prevent file operations using the given handle from modifying the last access time, call SetFileTime immediately after opening the file handle and pass a FILETIME structure whose dwLowDateTime and dwHighDateTime members are both set to 0xFFFFFFFF.

lpLastWriteTime [in, optional]

A pointer to a FILETIME structure that contains the new last modified date and time for the file or directory. This parameter can be NULL if the application does not need to change this information.

To prevent file operations using the given handle from modifying the last access time, call SetFileTime immediately after opening the file handle and pass a FILETIME structure whose dwLowDateTime and dwHighDateTime members are both set to 0xFFFFFFFF.

Return value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

Not all file systems can record creation and last access times and not all file systems record them in the same manner. For example, on FAT, create time has a resolution of 10 milliseconds, write time has a resolution of 2 seconds, and access time has a resolution of 1 day (really, the access date). Therefore, the GetFileTime function may not return the same file time information set using SetFileTime. NTFS delays updates to the last access time for a file by up to one hour after the last access.

Examples

For an example, see Changing a File Time to the Current Time.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winbase.h (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

See also

File Times
FILETIME
GetFileSize
GetFileTime
GetFileType
SetFileInformationByHandle
Time Functions

 

 

Send comments about this topic to Microsoft

Build date: 3/6/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Inconsistent behavior with documented preserve access time method

  

It seems to me like the “disablelastaccesstime” setting has an impact as to whether or not the SetFileTime 0xffffffff trick works on remote UNC files in non-intuitive ways.

I haven't tested every scenario, but what appears to be happening is that if disablelastaccess = 1 on a Win2k8r2 client, the last access time is getting updated at the target system.  If it is 0, it works as documented, e.g.

Client                                  File Host                  Result
Win2k8r2 with disablelastaccess = 1     Win2k3 with DLA = 0        FAIL: access time updated
Win2k8r2 with disablelastaccess = 0     Win2k3 with DLA = 0        GOOD: access time preserved
Win2k8r2 with disablelastaccess = 1     Win2k8r2 with DLA = 0      FAIL: access time updated
Has anybody else noticed this?
Avoiding update of last write time

You can use -1 for the last write time to indicate that operations on the current handle should not affect the last write time. (This behavior is documented in the WDK, Install File System section, and the parallel behavior for last access time is mentioned above.)

In addition, specifying a zero value for one of the parameters has the same effect as specifying NULL.

Convertation between time formats
You can use the SystemTimeToFileTime function to convert a SYSTEMTIME to a FILETIME. The SYSTEMTIME must be in UTC format. To convert a local time to a SYSTEMTIME in UTC Format use the TzSpecificLocalTimeToSystemTime function.
Managed Code
In managed code use System.IO.File.Set..Time instead.