Modifica dell'ora di un file all'ora corrente

Nell'esempio seguente viene impostata l'ora dell'ultima scrittura per un file sull'ora di sistema corrente usando la funzioneSetFileTime.

Il file system NTFS archivia i valori di ora in formato UTC, pertanto non sono interessati dalle modifiche apportate al fuso orario o all'ora legale. Il file system FAT archivia i valori di ora in base all'ora locale del computer.

Il file deve essere aperto con la funzione CreateFile usando l'accesso FILE_WRITE_ATTRIBUTES.

#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;
}