Home > Software engineering >  SetFileTime pit, time set not bottom go to, who stepped on? Can you tell me what's the matter?
SetFileTime pit, time set not bottom go to, who stepped on? Can you tell me what's the matter?

Time:01-16

Seems to be some time (legal) setting not bottom go to, to keep the original file time,
Boolean modifyWriteTime (string path1, string path2) {
FILETIME ftCreate1, ftAccess1 ftWrite1;
FILETIME ftCreate2, ftAccess2 ftWrite2;
FILETIME ftCreate3, ftAccess3 ftWrite3;
HANDLE fh1=CreateFile ((cstrings) path1. C_str (), GENERIC_READ,
FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, 0);

if (! GetFileTime (fh1, & amp; FtCreate1, & amp; FtAccess1, & amp; FtWrite1)) {
The CloseHandle (fh1);
return FALSE;
}

HANDLE fh2=CreateFile ((cstrings) 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, & amp; FtCreate2, & amp; FtAccess2, & amp; FtWrite2)) {
The CloseHandle (fh1);
return FALSE;
}
//set the time parameter (file handle, creation time, modify time)
DWORD r1=GetLastError ();
//ftWrite1. DwHighDateTime=30786615;
//ftWrite1. DwLowDateTime=3460456960;
BOOL retModTime=SetFileTime (fh2, & amp; FtCreate1, & amp; FtAccess1, & amp; FtWrite1);
DWORD r2=GetLastError ();
if (! RetModTime) {
The CloseHandle (fh1);
The CloseHandle (fh2);
return FALSE;
}
The CloseHandle (fh1);
The CloseHandle (fh2);

HANDLE fh3=CreateFile ((cstrings) 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, & amp; FtCreate3, & amp; FtAccess3, & amp; FtWrite3)) {
The CloseHandle (fh3);
return FALSE;
}
The CloseHandle (fh3);
return true;
}
FtWrite3 time and ftWrite2 is consistent, can't and ftWrite1 agree, however, the value of the modified ftWrite2 (commented out two lines), can be set down,
There is no error and failure,
Doubt is not a time to set down in the past?

CodePudding user response:

The information on the MSDN
Changing a File Time to the Current Time
The following example sets The last - write The time for a file to The current system time using The SetFileTime function. Note that the file must be the opened with the CreateFile function using FILE_WRITE_ATTRIBUTES access.

 
#include

//SetFileToCurrentTime - sets the last write time to the current system time
//the Return value - TRUE if successful, or FALSE otherwise
//hFile - must be a valid file handle

BOOL SetFileToCurrentTime (HANDLE hFile)
{
FILETIME ft;
SYSTEMTIME st;
BOOL f;

GetSystemTime (& amp; St);//gets the current time
SystemTimeToFileTime (& amp; St, & amp; Ft);//converts to file time format
F=SetFileTime (hFile,//sets the last - the write time for file
(LPFILETIME) NULL, (LPFILETIME) NULL, & amp; Ft);

Return the f;
}


  • Related