简介
修改文件创建时间、修改时间、大小等属性。
博客 《C++ 获取文件创建时间、修改时间、大小等属性》分享后,好兄弟“古月”发来一段代码,说可以修改文件的创建时间等。测试了一下真可以,下面是运行效果和代码:
代码
#include <windows.h>
#include <fstream>
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
SYSTEMTIME createTime; //创建时间
GetSystemTime(&createTime);
createTime.wDay = 9;
createTime.wMonth = 02;
createTime.wYear = 1923;
createTime.wHour = 1;
createTime.wMinute = 1;
createTime.wSecond = 7;
SYSTEMTIME lastWriteTime; //修改时间
GetSystemTime(&lastWriteTime);
lastWriteTime.wDay = 9;
lastWriteTime.wMonth = 2;
lastWriteTime.wYear = 1923;
lastWriteTime.wHour = 9;
lastWriteTime.wMinute = 37;
lastWriteTime.wSecond = 23;
SYSTEMTIME lastAccessTime; //访问时间
GetSystemTime(&lastAccessTime);
lastAccessTime.wDay = 9;
lastAccessTime.wMonth = 2;
lastAccessTime.wYear = 1923;
lastAccessTime.wHour = 15;
lastAccessTime.wMinute = 31;
lastAccessTime.wSecond = 8;
FILETIME lastWriteFiletime;
SystemTimeToFileTime(&lastWriteTime, &lastWriteFiletime);
FILETIME createFiletime;
SystemTimeToFileTime(&createTime, &createFiletime);
FILETIME lastAccessFileTime;
SystemTimeToFileTime(&lastAccessTime, &lastAccessFileTime);
HANDLE filename = CreateFile(L"E:\\LiHai123.txt", FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
// 设置文件的时间属性为之前设定的创建时间、最后访问时间和最后修改时间
SetFileTime(filename, &createFiletime, &lastAccessFileTime, &lastWriteFiletime);
CloseHandle(filename);
return 0;
}