首页 服务器 编程 必备知识 搜索引擎 圩日手册
站内搜索
最近浏览
推荐文章
热文排行

VC++ 获取文件的创建、修改、访问时间


由于在Windows系统中,FAT与NTFS保存文件时间的格式不相同:

FAT records times on disk in local time. However, NTFS records times on disk in UTC.

故必须按如下步骤,获取文件时间,仅参考:

HANDLE hFile = CreateFile(strFilePathName, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE,NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

if(hFile != INVALID_HANDLE_VALUE)

{

    FILETIME ftCreate,ftModify,ftAccess,ftLocal;

    SYSTEMTIME sysTime;

    GetFileTime(hFile, &ftCreate, &ftAccess, &ftModify); //

    FileTimeToLocalFileTime(&ftCreate, &ftLocal); //

    FileTimeToSystemTime(&ftLocal, &sysTime);

    CString strHint = "";

    CString strOemTime = "";

    strHint.Format("%d年%d月%d日, %d:%d:%d:%d", sysTime.wYear, sysTime.wMonth,sysTime.wDay,sysTime.wHour,sysTime.wMinute, sysTime.wSecond,sysTime.wMilliseconds);

    strOemTime.Format("%04d%02d%02d%02d%02d%02d%03d", sysTime.wYear, sysTime.wMonth,sysTime.wDay,sysTime.wHour,sysTime.wMinute, sysTime.wSecond,sysTime.wMilliseconds);

    strHint += ("\r\n" + strOemTime);

    SetDlgItemText(IDC_EDIT_CREATETIME, strHint);

    strHint.Empty(); //

    FileTimeToLocalFileTime(&ftModify, &ftLocal); //

    FileTimeToSystemTime(&ftLocal, &sysTime);

    strHint.Empty();

    strHint.Format("%d年%d月%d日, %d:%d:%d:%d", sysTime.wYear, sysTime.wMonth,sysTime.wDay,sysTime.wHour,sysTime.wMinute, sysTime.wSecond,sysTime.wMilliseconds);

    strOemTime.Empty();

    strOemTime.Format("%04d%02d%02d%02d%02d%02d%03d", sysTime.wYear, sysTime.wMonth,sysTime.wDay,sysTime.wHour,sysTime.wMinute, sysTime.wSecond,sysTime.wMilliseconds);

    strHint += ("\r\n" + strOemTime);

    SetDlgItemText(IDC_EDIT_MODIFYTIMER, strHint);

   strHint.Empty(); //

    FileTimeToLocalFileTime(&ftAccess, &ftLocal); //

    FileTimeToSystemTime(&ftLocal, &sysTime);

    strHint.Empty();

    strHint.Format("%d年%d月%d日, %d:%d:%d:%d", sysTime.wYear, sysTime.wMonth,sysTime.wDay,sysTime.wHour,sysTime.wMinute, sysTime.wSecond,sysTime.wMilliseconds);

    strOemTime.Empty();

    strOemTime.Format("%04d%02d%02d%02d%02d%02d%03d", sysTime.wYear, sysTime.wMonth,sysTime.wDay,sysTime.wHour,sysTime.wMinute, sysTime.wSecond,sysTime.wMilliseconds);

    strHint += ("\r\n" + strOemTime);

    SetDlgItemText(IDC_EDIT_ACCESSTIME, strHint);

    CloseHandle(hFile);

}

[wangjy17908]
添加时间:2009-08-09
版权所有(C)2005-2015