Modify the Windows platform to create a file, access, modification date and time properties

    Recently doing a P2P-based directory files automatically sync software development environment using VC ++ 2003, mainly FRAMEWORK to achieve cross-platform use with QT instead of MFC (in fact, not MFC), the communication part is mainly used UDT + TCP, UDT is based on UDP and implements the application layer protocol for reliable transmission, but also because with UDT achieve its penetration of NAT provides a theoretical feasibility (based on UDP, TCP is not to say not holes, but to achieve a relatively complex and low success rate ).
    Although QT offers as many features, I also try to provide a QT functions and not some platform-specific functions, QT, after all, is not a panacea, but I was doing last modification date and time modify the file (because the synchronization time to determine the file was last modified date and time, thus the existing file synchronization reasonable) when searched QT (I use QT 3.3.4, the latest and perhaps QT4 provide it) did not find such functionality. No way, this is only part of their own to achieve cross-platform. This feature is independent of the interface, compile-time macros to determine the current platform, then you can use a platform-independent function. Because I am not familiar with WINAPI indeed, hard to find WINAPI function called SetFileTime view MSDN find use as SetFileTime accept that type FILETIME, FILETIME specific date and time can not be modified directly, so the definition of a variable SYSTEMTIME, set up date and time, and then converted to a SYSTEMTIME SystemTimeToFileTime the FILETIME type, finally SetFileTime. I thought that's it, the results of debugging time is always kept in sync updates, I find it strange, presumably after some time, the files in the directory synchronization of all machines should be stable, not in the update. And initially suspected to be a logical question, but the track for a long time did not find the problem, and finally found documents calling SetFileTime all, a modification date more than I set eight hours, local think China is GMT + 8. So look MSDN, I found LocalFileTimeToFileTime also need to call the current local time into UTC time. Test again everything is normal!

Specific code similar to the following:

None.gif #ifdef WIN32    
None.gif            System Time Systime;
None.gif            FILE TIME ft, ftUTC;
None.gif            HANDLE hFile;
None.gif
None.gif            systime.wYear 
=  l_year;
None.gif            systime.wMonth 
=  l_month;
None.gif            systime.wDay 
=  l_day;
None.gif            systime.wHour 
=  l_hour;
None.gif            systime.wMinute 
=  l_minute;
None.gif            systime.wSecond 
=  l_second;
None.gif            systime.wMilliseconds 
=  l_millsecond;                
None.gif
None.gif            SystemTimeToFileTime (
& Systime,  & ft);
None.gif            LocalFileTimeToFileTime(
& ft, & ftUTC);
None.gif
None.gif            hFile 
=  CreateFile( filePathName, GENERIC_READ  |  GENERIC_WRITE,
None.gif                FILE_SHARE_READ
|  FILE_SHARE_WRITE,
None.gif                NULL,
None.gif                OPEN_EXISTING,
None.gif                FILE_ATTRIBUTE_NORMAL,
None.gif                NULL);            
None.gif            SetFileTime(hFile, (LPFILETIME) NULL, (LPFILETIME) NULL, 
& ftUTC);
None.gif            CloseHandle(hFile);
None.gif
#endif



Reproduced in: https: //www.cnblogs.com/leaway/archive/2007/01/22/626437.html

Guess you like

Origin blog.csdn.net/weixin_34228387/article/details/93840422