将文件时间更改为当前时间
以下示例使用 SetFileTime 函数将文件的上次写入时间设置为当前系统时间。
NTFS 文件系统以 UTC 格式存储时间值,因此它们不受时区或夏令时更改的影响。 FAT 文件系统根据计算机的本地时间存储时间值。
必须使用 createFile 函数使用 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;
}
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈