引言
应用做文件处理后,输出文件时间为当前的系统时间,但往往我们需要被创建的文件具有指定的创建日期、最后的修改日期和最后的访问日期。这里就使用win系统函数SetFileTime设置文件时间信息做完整实例分析。
SetFileTime函数说明
MSDN中对于函数接口有完整介绍,这里需要向函数传递文件句柄、创建时间、最后访问时间和最后修改时间。
BOOL WINAPI SetFileTime(
_In_ HANDLE hFile,
_In_opt_ const FILETIME *lpCreationTime,
_In_opt_ const FILETIME *lpLastAccessTime,
_In_opt_ const FILETIME *lpLastWriteTime
);
- 获得文件句柄
提示:这里并不是创建文件,而是获得已创建的文件句柄。
//getthe handle to the file
HANDLE filename = CreateFile("C:\\Users\\Lily\\Desktop\\varuntest.txt", FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
- 系统时间对象,设置时间
SYSTEMTIME createTime;
GetSystemTime(&createTime);
createTime.wDay = 3; //changes the day
createTime.wMonth = 01; //changes the month
createTime.wYear = 1921; //changes the year
createTime.wHour = 1; //changes the hour
createTime.wMinute = 1; //changes the minute
createTime.wSecond = 7; //changes the second
- 文件时间对象,将时间对象赋值给文件时间
FILETIME createFiletime;
SystemTimeToFileTime(&createTime, &createFiletime);
- 设置文件时间
//set the filetime on the file
SetFileTime(filename, &createFiletime, &lastAccessFileTime, &lastWriteFiletime);
- 关闭文件句柄
CloseHandle(filename);
完整代码
#include <windows.h>
#include <fstream>
#include <iostream>
using namespace std;
int main(int argc, char** argv) {
std::cout << "A new project." << endl;
//creates a fie as varuntest.txt
ofstream file("C:\\Users\\Lily\\Desktop\\varuntest.txt", ios::app);
SYSTEMTIME createTime;
GetSystemTime(&createTime);
createTime.wDay = 3; //changes the day
createTime.wMonth = 01; //changes the month
createTime.wYear = 1921; //changes the year
createTime.wHour = 1; //changes the hour
createTime.wMinute = 1; //changes the minute
createTime.wSecond = 7; //changes the second
SYSTEMTIME lastWriteTime;
GetSystemTime(&lastWriteTime);
lastWriteTime.wDay = 07; //changes the day
lastWriteTime.wMonth = 04; //changes the month
lastWriteTime.wYear = 2012; //changes the year
lastWriteTime.wHour = 9; //changes the hour
lastWriteTime.wMinute = 37; //changes the minute
lastWriteTime.wSecond = 23; //changes the second
SYSTEMTIME lastAccessTime;
GetSystemTime(&lastAccessTime);
lastAccessTime.wDay = 20; //changes the day
lastAccessTime.wMonth = 07; //changes the month
lastAccessTime.wYear = 2017; //changes the year
lastAccessTime.wHour = 15; //changes the hour
lastAccessTime.wMinute = 31; //changes the minute
lastAccessTime.wSecond = 8; //changes the second
//creation of a filetimestruct and convert our new systemtime
FILETIME lastWriteFiletime;
SystemTimeToFileTime(&lastWriteTime, &lastWriteFiletime);
FILETIME createFiletime;
SystemTimeToFileTime(&createTime, &createFiletime);
FILETIME lastAccessFileTime;
SystemTimeToFileTime(&lastAccessTime, &lastAccessFileTime);
//getthe handle to the file
HANDLE filename = CreateFile("C:\\Users\\Lily\\Desktop\\varuntest.txt", FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
//set the filetime on the file
SetFileTime(filename, &createFiletime, &lastAccessFileTime, &lastWriteFiletime);
//close our handle.
CloseHandle(filename);
return 0;
}
示例代码创建的文件
将文件路径修改为指向已经存在的文件,得到的文件时间参数(其中人为访问过一次)
上述代码编译会提示错误
e:\shizhenwei\vs2013projects\testsetfiletime\testsetfiletime\main.cpp(50): error C2664: “HANDLE CreateFileW(LPCWSTR,DWORD,DWORD,LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE)”: 无法将参数 1 从“const char [36]”转换为“LPCWSTR”
1> 与指向的类型无关;转换要求 reinterpret_cast、C 样式转换或函数样式转换
解决方法:
选中项目,右键属性——>配置属性——>常规——>项目默认值——>字符集,选为“使用多字节字符集”。
总结
在win下开发,上述代码可直接使用。
参考
https://msdn.microsoft.com/en-us/library/ms724933(VS.85).aspx
https://stackoverflow.com/questions/10041651/changing-the-file-creation-date-in-c-using-windows-h-in-windows-7
http://blog.csdn.net/chw1989/article/details/7482205