標籤:api 獲得 pcr orm 修改 stdio.h pat 目錄 min
FILETIME結構包含了檔案或目錄的日期和時間資訊:(自1601年1月1日以來,單位為100納秒)
typedef struct _FILETIME { DWORD dwLowDateTime; //低32位 DWORD dwHighDateTime; //高32位} FILETIME, *PFILETIME;
SYSTEMTIME結構包含了使用者可識別的系統日期資訊:
typedef struct _SYSTEMTIME { WORD wYear;//年 WORD wMonth;//月 WORD wDayOfWeek;//一周的第幾天 WORD wDay;//日 WORD wHour;//小時 WORD wMinute;//分 WORD wSecond;//秒 WORD wMilliseconds;//毫秒} SYSTEMTIME, *PSYSTEMTIME;
=======================================================
函數FileTimeToSystemTime用來將檔案時間格式轉換為標準系統時間格式:
BOOL WINAPI FileTimeToSystemTime( __in const FILETIME *lpFileTime, //檔案時間 __out LPSYSTEMTIME lpSystemTime //系統時間);
函數FileTimeToLocalTime用來將檔案時間格式轉換為本地檔案時間:
BOOL WINAPI FileTimeToLocalFileTime( __in const FILETIME* lpFileTime,//檔案時間 __out LPFILETIME lpLocalFileTime//本地檔案時間);
函數SystemTimeToFileTime則是將標準系統時間轉換成檔案時間格式:
BOOL WINAPI SystemTimeToFileTime( __in const SYSTEMTIME *lpSystemTime,//系統時間 __out LPFILETIME lpFileTime//檔案時間 );
函數SystemTimeToTzSpecificLocalTime是將標準系統時間轉換為本地系統時間
BOOL WINAPI SystemTimeToTzSpecificLocalTime( __in LPTIME_ZONE_INFORMATION lpTimeZone,//時區結構 __in LPSYSTEMTIME lpUniversalTime,//系統時間 __out LPSYSTEMTIME lpLocalTime//本地時間);
=======================================================
GetSystemTime函數用來獲得系統時間:
void WINAPI GetSystemTime( __out LPSYSTEMTIME lpSystemTime );
GetFileTime函數用來獲得一個檔案或目錄的建立的時間、最後訪問的時間以及最後修改的時間:
BOOL WINAPI GetFileTime( __in HANDLE hFile, //檔案或目錄控制代碼 __out_opt LPFILETIME lpCreationTime, //返回的建立的日期和時間資訊 __out_opt LPFILETIME lpLastAccessTime, //返回的最後訪問的日期和時間資訊 __out_opt LPFILETIME lpLastWriteTime //返回的最後修改的日期和時間資訊);
執行個體:
CString strPath("D:\\test.txt");HANDLE hFile = CreateFile(strPath, GENERIC_WRITE| GENERIC_READ, //必須有GENERIC_READ屬性才能得到時間 FILE_SHARE_READ, NULL, TRUNCATE_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile != INVALID_HANDLE_VALUE) { SYSTEMTIME sysTime; GetSystemTime(&sysTime);//這裡得到的時間是標準系統時間,也就是0時區的時間。 GetLocalTime(&sysTime);//這裡得到的是本地時間,也就是標準時間+時區時間 FILETIME fCreateTime, fAccessTime, fWriteTime; GetFileTime(&hFile, &fCreateTime, &fAccessTime, &fWriteTime);//擷取檔案時間 CString strTime;//將檔案時間轉換為本地系統時間的兩種方式://(1) FileTimeToLocalFileTime(&fCreateTime,&localTime);//將檔案時間轉換為本地檔案時間 FileTimeToSystemTime(&localTime, &sysTime);//將檔案時間轉換為本地系統時間//(2) FileTimeToSystemTime(&fCreateTime, &sysTime);//將檔案時間轉換為標準系統時間 SystemTimeToTzSpecificLocalTime(&sysTime, &sysTime)//將標準系統時間轉換為本地系統時間 strTime.Format(_T("%4d年%2d月%2d日,%2d:%2d:%2d"), sysTime.wYear, sysTime.wMonth, sysTime.wDay, sysTime.wHour, sysTime.wMinute, sysTime.wSecond );}
修檔案建立時間,例子:
#include <Windows.h> #include <stdio.h> bool ConvertFileTimeToLocalTime(const FILETIME *lpFileTime, SYSTEMTIME *lpSystemTime) { if (!lpFileTime || !lpSystemTime) { return false; } FILETIME ftLocal; FileTimeToLocalFileTime(lpFileTime, &ftLocal); FileTimeToSystemTime(&ftLocal, lpSystemTime); return true; } bool ConvertLocalTimeToFileTime(const SYSTEMTIME *lpSystemTime, FILETIME *lpFileTime) { if (!lpSystemTime || !lpFileTime) { return false; } FILETIME ftLocal; SystemTimeToFileTime(lpSystemTime, &ftLocal); LocalFileTimeToFileTime(&ftLocal, lpFileTime); return true; } int main() { HANDLE hFile; FILETIME ftCreate, ftAccess, ftWrite; SYSTEMTIME stCreate, stAccess, stWrite; int year, month, day; hFile = CreateFile(L"C:\\1.txt", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); if (INVALID_HANDLE_VALUE == hFile) { printf("CreateFile error: %d", GetLastError()); ExitProcess(0); } GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite); ConvertFileTimeToLocalTime(&ftCreate, &stCreate); ConvertFileTimeToLocalTime(&ftAccess, &stAccess); ConvertFileTimeToLocalTime(&ftWrite, &stWrite); printf("yyyy-MM-dd:"); scanf("%d-%d-%d", &year, &month, &day); stAccess.wYear = stWrite.wYear = year; stAccess.wMonth = stWrite.wMonth = month; stAccess.wDay = stWrite.wDay = day; ConvertLocalTimeToFileTime(&stAccess, &ftAccess); ConvertLocalTimeToFileTime(&stWrite, &ftWrite); SetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite); CloseHandle(hFile); return 0; }
WINDOWS API ——GETFILETIME——擷取檔案時間