Mr.*_*Boy 2 c++ winapi visual-c++ visual-studio-2013
我正在用来::CopyFile()制作文件的副本。看来原始文件的时间戳被保留,我希望副本在副本上设置当前时间戳,即“触摸”它。
有没有 WinAPI 方法可以轻松做到这一点?
如果您阅读了的MSDN 文档CopyFile(),底部的注释如下:
文件时间语义
本文应该记录有关文件创建/修改/访问时间的语义。创建时间:如果目标文件已经存在,则保留其创建时间,否则设置为当前系统时间。
最后修改时间:始终从源文件的修改时间复制。
上次访问时间:始终设置为当前系统时间。
和
修改时间并不总是保留
不保证设置修改时间。CopyFileEx 确实尝试设置修改时间,但它不会对其进行错误检查。这意味着如果在 CopyFileEx 内部设置修改时间失败(例如访问被拒绝),后者仍将返回成功!因此,如果修改时间对于您的场景很重要(对于我的同步程序),您必须显式调用 SetFileTime() 并检查它的返回值以确保确定。
您应该SetFileTime()自己更新复制文件的时间戳,以确保它们设置为您想要的设置。MSDN上有一个例子:
#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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2180 次 |
| 最近记录: |