VC.文件时间
1、
#include <stdio.h> #include <windows.h> void GetFileTimeZ(char *_pcFullFileName, FILETIME *_pftCreateTime, FILETIME *_pftAccessTime, FILETIME *_pftWriteTime); void PrintFileTimeZ(FILETIME *_pftCreateTime, FILETIME *_pftAccessTime, FILETIME *_pftWriteTime); void SetFileTimeZ(char *_pcFullFileName, FILETIME *_pftCreateTime, FILETIME *_pftAccessTime, FILETIME *_pftWriteTime); int main(int _argc, char** _argv) { // 参数:获取 文件"_argv[1]"的创建/访问/写入时间,设置到 文件"_argv[2]"上面去。 printf("_argc : %d\n", _argc); printf("_argv[1] : %s\n", _argv[1]); // for (int i=0; i<_argc; i++) // printf("%s\n", _argv[i]); if (_argc <= 1) { printf("_argc : %d\n", _argc); return 0; } printf("\n"); FILETIME ftCreateTime={0}, ftAccessTime={0}, ftWriteTime={0}; GetFileTimeZ(_argv[1], &ftCreateTime, &ftAccessTime, &ftWriteTime); SetFileTimeZ(_argv[2], &ftCreateTime, &ftAccessTime, &ftWriteTime); return 0; } /// *** void GetFileTimeZ(char *_pcFullFileName, FILETIME *_pftCreateTime, FILETIME *_pftAccessTime, FILETIME *_pftWriteTime) { HANDLE hFile = CreateFile(_pcFullFileName, GENERIC_WRITE | GENERIC_READ, //必须有GENERIC_READ属性才能得到时间 FILE_SHARE_READ, NULL, OPEN_EXISTING,//TRUNCATE_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { printf("CreateFile return INVALID_HANDLE_VALUE, GetLastError() : %d\n", GetLastError()); return; } if ( ! GetFileTime(hFile, _pftCreateTime, _pftAccessTime, _pftWriteTime) )//获取文件时间 { printf("GetFileTime failed, GetLastError() : %d\n", GetLastError()); CloseHandle(hFile); return; } CloseHandle(hFile); PrintFileTimeZ(_pftCreateTime, _pftAccessTime, _pftWriteTime); } void PrintFileTimeZ(FILETIME *_pftCreateTime, FILETIME *_pftAccessTime, FILETIME *_pftWriteTime) { SYSTEMTIME st = {0}; SYSTEMTIME stLocal = {0}; GetSystemTime(&st);// 这里得到的时间是标准系统时间,也就是0时区的时间。 GetLocalTime(&stLocal);// 这里得到的是本地时间,也就是标准时间+时区时间 printf("Local time (now) : \n\t Year:%d, Month:%d, (DayOfWeek:%d,) Day:%d \n\t Hour:%d, Minute:%d, Second:%d, Milliseconds:%d\n", stLocal.wYear, stLocal.wMonth, stLocal.wDayOfWeek, stLocal.wDay, stLocal.wHour, stLocal.wMinute, stLocal.wSecond, stLocal.wMilliseconds); printf("System time (now) : \n\t Year:%d, Month:%d, (DayOfWeek:%d,) Day:%d \n\t Hour:%d, Minute:%d, Second:%d, Milliseconds:%d\n", stLocal.wYear, stLocal.wMonth, stLocal.wDayOfWeek, stLocal.wDay, stLocal.wHour, stLocal.wMinute, stLocal.wSecond, stLocal.wMilliseconds); printf("\n"); // //将文件时间转换为本地系统时间的两种方式: // //(1) // FileTimeToLocalFileTime(&fCreateTime,&localTime);//将文件时间转换为本地文件时间 // FileTimeToSystemTime(&localTime, &sysTime);//将文件时间转换为本地系统时间 // // //(2) // FileTimeToSystemTime(&fCreateTime, &sysTime);//将文件时间转换为标准系统时间 // SystemTimeToTzSpecificLocalTime(&sysTime, &sysTime)//将标准系统时间转换为本地系统时间 FILETIME ftLocal = {0}; if (_pftCreateTime != NULL) { FileTimeToLocalFileTime(_pftCreateTime, &ftLocal);//将文件时间转换为本地文件时间 FileTimeToSystemTime(&ftLocal, &stLocal); printf("CreateTime (file) : \n\t Year:%d, Month:%d, (DayOfWeek:%d,) Day:%d \n\t Hour:%d, Minute:%d, Second:%d, Milliseconds:%d\n", stLocal.wYear, stLocal.wMonth, stLocal.wDayOfWeek, stLocal.wDay, stLocal.wHour, stLocal.wMinute, stLocal.wSecond, stLocal.wMilliseconds); } if (_pftAccessTime != NULL) { FileTimeToLocalFileTime(_pftAccessTime, &ftLocal);//将文件时间转换为本地文件时间 FileTimeToSystemTime(&ftLocal, &stLocal); printf("AccessTime (file) : \n\t Year:%d, Month:%d, (DayOfWeek:%d,) Day:%d \n\t Hour:%d, Minute:%d, Second:%d, Milliseconds:%d\n", stLocal.wYear, stLocal.wMonth, stLocal.wDayOfWeek, stLocal.wDay, stLocal.wHour, stLocal.wMinute, stLocal.wSecond, stLocal.wMilliseconds); } if (_pftWriteTime != NULL) { FileTimeToLocalFileTime(_pftWriteTime, &ftLocal);//将文件时间转换为本地文件时间 FileTimeToSystemTime(&ftLocal, &stLocal); printf("WriteTime (file) : \n\t Year:%d, Month:%d, (DayOfWeek:%d,) Day:%d \n\t Hour:%d, Minute:%d, Second:%d, Milliseconds:%d\n", stLocal.wYear, stLocal.wMonth, stLocal.wDayOfWeek, stLocal.wDay, stLocal.wHour, stLocal.wMinute, stLocal.wSecond, stLocal.wMilliseconds); } } void SetFileTimeZ(char *_pcFullFileName, FILETIME *_pftCreateTime, FILETIME *_pftAccessTime, FILETIME *_pftWriteTime) { HANDLE hFile = CreateFile(_pcFullFileName, GENERIC_WRITE | GENERIC_READ, //必须有GENERIC_READ属性才能得到时间 FILE_SHARE_READ, NULL, OPEN_EXISTING,//TRUNCATE_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) { printf("CreateFile return INVALID_HANDLE_VALUE, GetLastError() : %d\n", GetLastError()); return; } if ( ! SetFileTime(hFile, _pftCreateTime, _pftAccessTime, _pftWriteTime) )//获取文件时间 { printf("SetFileTime failed, GetLastError() : %d\n", GetLastError()); CloseHandle(hFile); return; } CloseHandle(hFile); }
2、
3、
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 大模型 Token 究竟是啥:图解大模型Token
· 35岁程序员的中年求职记:四次碰壁后的深度反思
· 继承的思维:从思维模式到架构设计的深度解析
· 如何在 .NET 中 使用 ANTLR4
· 后端思维之高并发处理方案
· BotSharp + MCP 三步实现智能体开发
· BotSharp 5.0 MCP:迈向更开放的AI Agent框架
· 分享 3 款基于 .NET 开源且免费的远程桌面工具
· 在线聊天系统中的多窗口数据同步技术解密
· 5. RabbitMQ 消息队列中 Exchanges(交换机) 的详细说明