C++ win环境修改文件创建时间、最后的修改时间和最后的访问时间

您所在的位置:网站首页 获取文件生成时间怎么设置 C++ win环境修改文件创建时间、最后的修改时间和最后的访问时间

C++ win环境修改文件创建时间、最后的修改时间和最后的访问时间

2024-07-17 13:51| 来源: 网络整理| 查看: 265

引言

应用做文件处理后,输出文件时间为当前的系统时间,但往往我们需要被创建的文件具有指定的创建日期、最后的修改日期和最后的访问日期。这里就使用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 #include #include using namespace std; int main(int argc, char** argv) { std::cout 配置属性——>常规——>项目默认值——>字符集,选为“使用多字节字符集”。

总结

在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



【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3