파일 시간을 현재 시간으로 변경

다음 예제에서는 SetFileTime 함수를 사용하여 파일의 마지막 쓰기 시간을 현재 시스템 시간으로 설정합니다.

NTFS 파일 시스템은 시간 값을 UTC 형식으로 저장하므로 표준 시간대 또는 일광 절약 시간제 변경의 영향을 받지 않습니다. FAT 파일 시스템은 컴퓨터의 현지 시간을 기준으로 시간 값을 저장합니다.

파일은 FILE_WRITE_ATTRIBUTES 액세스를 사용하여 CreateFile 함수로 열어야 합니다.

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