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 ", _argc); printf("_argv[1] : %s ", _argv[1]); // for (int i=0; i<_argc; i++) // printf("%s ", _argv[i]); if (_argc <= 1) { printf("_argc : %d ", _argc); return 0; } printf(" "); 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 ", GetLastError()); return; } if ( ! GetFileTime(hFile, _pftCreateTime, _pftAccessTime, _pftWriteTime) )//获取文件时间 { printf("GetFileTime failed, GetLastError() : %d ", 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) : Year:%d, Month:%d, (DayOfWeek:%d,) Day:%d Hour:%d, Minute:%d, Second:%d, Milliseconds:%d ", stLocal.wYear, stLocal.wMonth, stLocal.wDayOfWeek, stLocal.wDay, stLocal.wHour, stLocal.wMinute, stLocal.wSecond, stLocal.wMilliseconds); printf("System time (now) : Year:%d, Month:%d, (DayOfWeek:%d,) Day:%d Hour:%d, Minute:%d, Second:%d, Milliseconds:%d ", stLocal.wYear, stLocal.wMonth, stLocal.wDayOfWeek, stLocal.wDay, stLocal.wHour, stLocal.wMinute, stLocal.wSecond, stLocal.wMilliseconds); printf(" "); // //将文件时间转换为本地系统时间的两种方式: // //(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) : Year:%d, Month:%d, (DayOfWeek:%d,) Day:%d Hour:%d, Minute:%d, Second:%d, Milliseconds:%d ", 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) : Year:%d, Month:%d, (DayOfWeek:%d,) Day:%d Hour:%d, Minute:%d, Second:%d, Milliseconds:%d ", 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) : Year:%d, Month:%d, (DayOfWeek:%d,) Day:%d Hour:%d, Minute:%d, Second:%d, Milliseconds:%d ", 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 ", GetLastError()); return; } if ( ! SetFileTime(hFile, _pftCreateTime, _pftAccessTime, _pftWriteTime) )//获取文件时间 { printf("SetFileTime failed, GetLastError() : %d ", GetLastError()); CloseHandle(hFile); return; } CloseHandle(hFile); }
2、
3、