WINDOWS API ——GETFILETIME——擷取檔案時間

來源:互聯網
上載者:User

標籤: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——擷取檔案時間

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.