Ändern einer Dateizeit in die aktuelle Zeit

Im folgenden Beispiel wird die Zeit des letzten Schreibvorgangs für eine Datei auf die aktuelle Systemzeit mithilfe der SetFileTime-Funktion festgelegt.

Das NTFS-Dateisystem speichert Zeitwerte im UTC-Format, sodass sie nicht von Änderungen in der Zeitzone oder sommerzeit beeinflusst werden. Das FAT-Dateisystem speichert Zeitwerte basierend auf der Ortszeit des Computers.

Die Datei muss mit der CreateFile-Funktion mit FILE_WRITE_ATTRIBUTES Zugriff geöffnet werden.

#include <windows.h>

// SetFileToCurrentTime - sets last write time to current system time
// Return value - TRUE if successful, FALSE otherwise
// hFile  - must be a valid file handle

BOOL SetFileToCurrentTime(HANDLE hFile)
{
    FILETIME ft;
    SYSTEMTIME st;
    BOOL f;

    GetSystemTime(&st);              // Gets the current system time
    SystemTimeToFileTime(&st, &ft);  // Converts the current system time to file time format
    f = SetFileTime(hFile,           // Sets last-write time of the file 
        (LPFILETIME) NULL,           // to the converted current system time 
        (LPFILETIME) NULL, 
        &ft);    

    return f;
}