Compartilhar via


Alterando um tempo de arquivo para a hora atual

O exemplo a seguir define a hora da última gravação de um arquivo para a hora atual do sistema usando a função SetFileTime .

O sistema de arquivos NTFS armazena valores de tempo no formato UTC, portanto, eles não são afetados por alterações no fuso horário ou horário de verão. O sistema de arquivos FAT armazena valores de tempo com base na hora local do computador.

O arquivo deve ser aberto com a função CreateFile usando FILE_WRITE_ATTRIBUTES acesso.

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