VC + + Get file attribute creation time, modification time, and access time

Source: Internet
Author: User
Tags filetime

Reprint: http://blog.sina.com.cn/s/blog_66bf8d8301014ikd.html

WIN32_FIND_DATA structure

All the properties of the file, the following 9 kinds of information: the title name of the file, the properties of the file (read-only, archive, hidden, etc.), the creation time of the file, the last access time of the file, the last modification time of the file, the high double word of the file size, the low double word of the file size Here only the file title name and the length of the file can be easily obtained through the CFile class, and for several other properties of the acquisition and settings are powerless.

The FindFirst () and FindNext () functions used to find disk files often use a data structure WIN32_FIND_DATA member variables contain all the above file attributes, so it can be used as a means to obtain and change file attributes. The contents of this structure are as follows:

typedef struct _WIN32_FIND_DATA {
DWORD dwfileattributes; File properties
FILETIME Ftcreationtime; File creation time
FILETIME Ftlastaccesstime; File last access time
FILETIME Ftlastwritetime; Last modified time of file
DWORD Nfilesizehigh; File length 32 bits high
DWORD Nfilesizelow; File length 32 bits Low
DWORD dwReserved0; System retention
DWORD dwReserved1; System retention
TCHAR cfilename[MAX_PATH]; Long File name
TCHAR calternatefilename[14]; 8.3 Format file name
} win32_find_data, *pwin32_find_data;

The FindFirstFile () function can be used to find the file based on the current file storage path to read the relevant properties of the file to be manipulated into the WIN32_FIND_DATA structure:

Win32_find_data FfD;
HANDLE hfind = FindFirstFile ("C:\test.dat", &FFD);

When using this structure, you cannot manually modify any of the data in this structure, and the structure can only be used as a read-only data for developers, and all of its member variables will be completed by the system.

Instance:

--get the LastWriteTime of Folder/files
Win32_find_data FfD;
HANDLE hfind = FindFirstFile (OPENEDSOURCEDIR,&FFD);
SYSTEMTIME STUTC, stlocal;
FileTimeToSystemTime (& (Ffd.ftlastwritetime), &STUTC);
Systemtimetotzspecificlocaltime (NULL, &STUTC, &stlocal);
Mytime.format ("%d.%d%d,%d:%d", Stlocal.wday,stlocal.wmonth,stlocal.wyear,stlocal.whour,stlocal.wminute);
//--
D_colorstatic.setwindowtext ((LPCTSTR) myTime);

2. Get file attribute creation time, modification time, and access time

Reprint: http://blog.csdn.net/awu999328/article/details/24470621

1 FILETIME ftcreate, ftmodify, ftaccess;2 CString strcreatetime, Strmodifytime, Straccesstime;3CString strFilePath = _t ("");4 5HANDLE hfile = CreateFile (strFilePath, Generic_read,//Open for reading6File_share_read,//Share for reading7Null//Default Security8Open_existing,//existing file only9File_flag_backup_semantics,//Normal fileTen NULL); One  A SYSTEMTIME stlocal; -  if(! Getfiletime (hfile, &ftcreate, &ftaccess, &ftmodify)) -  { the   return ; -  } -  -ZeroMemory (&stlocal,sizeof(SYSTEMTIME)); +FileTimeToSystemTime (&ftcreate, &stlocal); -Strcreatetime.format ("%04d-%02d-%02d%02d:%02d:%02d", Stlocal.wyear, Stlocal.wmonth, Stlocal.wday, Stlocal.whour, Stlocal.wminute, Stlocal.wsecond);//File creation Time +ZeroMemory (&stlocal,sizeof(SYSTEMTIME)); AFileTimeToSystemTime (&ftmodify, &stlocal); atStrmodifytime.format ("%04d-%02d-%02d%02d:%02d:%02d", Stlocal.wyear, Stlocal.wmonth, Stlocal.wday, Stlocal.whour, Stlocal.wminute, Stlocal.wsecond);//File modification Time -ZeroMemory (&stlocal,sizeof(SYSTEMTIME)); -FileTimeToSystemTime (&ftaccess, &stlocal); -Straccesstime.format ("%04d-%02d-%02d%02d:%02d:%02d", Stlocal.wyear, Stlocal.wmonth, Stlocal.wday, Stlocal.whour, Stlocal.wminute, Stlocal.wsecond);//File access Time

The CreateFile here only causes the file to be read-only and cannot have other permissions, preferably with FindFile for creation, modification, and access time

VC + + Get file attribute creation time, modification time, and access time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.