VC++ 获取文件属性创建时间、修改时间和访问时间

来源:互联网 发布:中华大字典软件 编辑:程序博客网 时间:2024/05/21 09:13

转自:http://hi.baidu.com/lcdkc/item/2010c0a999ac29dc5af1917c


FILETIME ftCreate, ftModify, ftAccess;
 CString strCreateTime, strModifyTime, strAccessTime;
 CString strFilePath = _T("");

 

 GetDlgItem(IDC_EDT_FILEPATH)->GetWindowText(strFilePath);  // 文件路径
 HANDLE hFile = CreateFile(strFilePath, GENERIC_READ,          // open for reading
  FILE_SHARE_READ,       // share for reading
  NULL,                            // default security
  OPEN_EXISTING,          // existing file only
  FILE_FLAG_BACKUP_SEMANTICS , // normal file
  NULL);

 SYSTEMTIME stLocal;
 if (!GetFileTime(hFile, &ftCreate, &ftAccess, &ftModify))
 {
  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);   // 文件创建时间
 ZeroMemory(&stLocal, sizeof(SYSTEMTIME));
 FileTimeToSystemTime(&ftModify, &stLocal);
 strModifyTime.Format("%04d-%02d-%02d %02d:%02d:%02d", stLocal.wYear, stLocal.wMonth, stLocal.wDay,  stLocal.wHour, stLocal.wMinute, stLocal.wSecond); //  文件修改时间
 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); // 文件访问时间

 SetDlgItemText(IDC_STC_CREATETIME, strCreateTime);
 SetDlgItemText(IDC_STC_MODIFYTIME, strModifyTime);
 SetDlgItemText(IDC_STC_ACCESSTIME, strAccessTime);


这里的CreateFile只导致文件只能被只读打开,不能有其他权限,最好用FindFile获取创建,修改和访问时间


0 0