Share via


Modifica di un'ora file all'ora corrente

L'esempio seguente imposta l'ora di ultima scrittura per un file all'ora di sistema corrente usando la funzione SetFileTime .

Il file system NTFS archivia i valori di ora in formato UTC, quindi 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 FILE_WRITE_ATTRIBUTES accesso.

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