得到文件的创建时间修改时间等

复制代码
#include <windows.h>
#include <tchar.h>
#include <strsafe.h>

// GetLastWriteTime - Retrieves the last-write time and converts//                    the time to a string//// Return value - TRUE if successful, FALSE otherwise// hFile      - Valid file handle// lpszString - Pointer to buffer to receive string

BOOL GetLastWriteTime(HANDLE hFile, LPTSTR lpszString, DWORD dwSize)
{
    FILETIME ftCreate, ftAccess, ftWrite;
    SYSTEMTIME stUTC, stLocal;
    DWORD dwRet;

    // Retrieve the file times for the file.if (!GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite))
        return FALSE;

    // Convert the last-write time to local time.
    FileTimeToSystemTime(&ftWrite, &stUTC);
    SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);

    // Build a string showing the date and time.
    dwRet = StringCchPrintf(lpszString, dwSize, 
        TEXT("%02d/%02d/%d  %02d:%02d"),
        stLocal.wMonth, stLocal.wDay, stLocal.wYear,
        stLocal.wHour, stLocal.wMinute);

    if( S_OK == dwRet )
        return TRUE;
    elsereturn FALSE;
}

int _tmain(int argc, TCHAR *argv[])
{
    HANDLE hFile;
    TCHAR szBuf[MAX_PATH];

    if( argc != 2 )
    {
        printf("This sample takes a file name as a parameter\n");
        return 0;
    }
    hFile = CreateFile(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL,
        OPEN_EXISTING, 0, NULL);

    if(hFile == INVALID_HANDLE_VALUE)
    {
        printf("CreateFile failed with %d\n", GetLastError());
        return 0;
    }
    if(GetLastWriteTime( hFile, szBuf, MAX_PATH ))
        _tprintf(TEXT("Last write time is: %s\n"), szBuf);
        
    CloseHandle(hFile);    
}
 
 
另外例子  觉得这个好
 
 
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);
复制代码

 

 
posted @   廖先生  阅读(607)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示