SetFileTime的坑,时间设不下去,谁踩到过?请问怎么回事?

hc_z 2021-01-15 04:50:16
貌似有些时间(合法的时间)设置不下去。保持原有文件时间。
boolean modifyWriteTime(string path1, string path2) {
FILETIME ftCreate1, ftAccess1, ftWrite1;
FILETIME ftCreate2, ftAccess2, ftWrite2;
FILETIME ftCreate3, ftAccess3, ftWrite3;
HANDLE fh1 = CreateFile((CString)path1.c_str(), GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, 0);

if (!GetFileTime(fh1, &ftCreate1, &ftAccess1, &ftWrite1)) {
CloseHandle(fh1);
return FALSE;
}

HANDLE fh2 = CreateFile((CString)path2.c_str(), GENERIC_ALL,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); // FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_WRITE_THROUGH
if (!GetFileTime(fh2, &ftCreate2, &ftAccess2, &ftWrite2)) {
CloseHandle(fh1);
return FALSE;
}
//设置时间 参数(文件句柄,创建时间,修改时间)
DWORD r1 = GetLastError();
//ftWrite1.dwHighDateTime = 30786615;
//ftWrite1.dwLowDateTime = 3460456960;
BOOL retModTime = SetFileTime(fh2, &ftCreate1, &ftAccess1, &ftWrite1);
DWORD r2 = GetLastError();
if (!retModTime) {
CloseHandle(fh1);
CloseHandle(fh2);
return FALSE;
}
CloseHandle(fh1);
CloseHandle(fh2);

HANDLE fh3 = CreateFile((CString)path2.c_str(), GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, 0); // FILE_FLAG_BACKUP_SEMANTICS
if (fh3 == NULL) {
return false;
}
ftWrite3.dwHighDateTime = 111;
ftWrite3.dwLowDateTime = 222;
if (!GetFileTime(fh3, &ftCreate3, &ftAccess3, &ftWrite3)) {
CloseHandle(fh3);
return FALSE;
}
CloseHandle(fh3);
return true;
}
ftWrite3取得的时间和ftWrite2一致,无法和ftWrite1一致。但是,修改ftWrite2的值(注释掉的两行),是可以设下去的。
没有报错和失败。
怀疑是不是不能把过去的时间设置下去?
...全文
243 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
hc_z 2021-01-18
  • 打赏
  • 举报
回复
有个新发现,操作硬盘上的文件没问题,操作SD卡的文件才出现无效的问题。
是权限问题?
hc_z 2021-01-18
  • 打赏
  • 举报
回复
会不会这个API在Win10上的问题?
hc_z 2021-01-18
  • 打赏
  • 举报
回复
也没有用啊。
HANDLE fh2 = CreateFile((CString)path2.c_str(), GENERIC_WRITE,
0, NULL, OPEN_EXISTING, FILE_WRITE_ATTRIBUTES, fh1);
zgl7903 2021-01-15
  • 打赏
  • 举报
回复
MSDN 上的说明 Changing a File Time to the Current Time The following example sets the last-write time for a file to the current system time using the SetFileTime function. Note that the file must be opened with the CreateFile function using FILE_WRITE_ATTRIBUTES access.

#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 current time
    SystemTimeToFileTime(&st, &ft);  // converts to file time format
    f = SetFileTime(hFile,           // sets last-write time for file
        (LPFILETIME) NULL, (LPFILETIME) NULL, &ft);

    return f;
}

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧