1. 程式人生 > >C++ win環境修改檔案建立時間、最後的修改時間和最後的訪問時間

C++ win環境修改檔案建立時間、最後的修改時間和最後的訪問時間

引言

應用做檔案處理後,輸出檔案時間為當前的系統時間,但往往我們需要被建立的檔案具有指定的建立日期、最後的修改日期和最後的訪問日期。這裡就使用win系統函式SetFileTime設定檔案時間資訊做完整例項分析。

SetFileTime函式說明

MSDN中對於函式介面有完整介紹,這裡需要向函式傳遞檔案控制代碼、建立時間、最後訪問時間和最後修改時間。

BOOL WINAPI SetFileTime(
  _In_           HANDLE   hFile,
  _In_opt_ const FILETIME *lpCreationTime,
  _In_opt_ const FILETIME *lpLastAccessTime,
  _In_opt_ const
FILETIME *lpLastWriteTime );
  • 獲得檔案控制代碼
    提示:這裡並不是建立檔案,而是獲得已建立的檔案控制代碼。
    //getthe handle to the file
    HANDLE filename = CreateFile("C:\\Users\\Lily\\Desktop\\varuntest.txt", FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  • 系統時間物件,設定時間
    SYSTEMTIME createTime;
    GetSystemTime(&createTime);
    createTime.wDay = 3;            //changes the day
    createTime.wMonth = 01;         //changes the month
    createTime.wYear = 1921;        //changes the year
    createTime.wHour = 1;           //changes the hour
    createTime.wMinute = 1
; //changes the minute createTime.wSecond = 7; //changes the second
  • 檔案時間物件,將時間物件賦值給檔案時間
    FILETIME createFiletime;
    SystemTimeToFileTime(&createTime, &createFiletime);
  • 設定檔案時間
    //set the filetime on the file
    SetFileTime(filename, &createFiletime, &lastAccessFileTime, &lastWriteFiletime);
  • 關閉檔案控制代碼
    CloseHandle(filename);

完整程式碼

#include <windows.h>
#include <fstream>
#include <iostream>
using namespace std;

int main(int argc, char** argv) {
    std::cout << "A new project." << endl;

    //creates a fie as varuntest.txt
    ofstream file("C:\\Users\\Lily\\Desktop\\varuntest.txt", ios::app);

    SYSTEMTIME createTime;
    GetSystemTime(&createTime);
    createTime.wDay = 3;            //changes the day
    createTime.wMonth = 01;         //changes the month
    createTime.wYear = 1921;        //changes the year
    createTime.wHour = 1;           //changes the hour
    createTime.wMinute = 1;         //changes the minute
    createTime.wSecond = 7;         //changes the second

    SYSTEMTIME lastWriteTime;
    GetSystemTime(&lastWriteTime);
    lastWriteTime.wDay = 07;        //changes the day
    lastWriteTime.wMonth = 04;      //changes the month
    lastWriteTime.wYear = 2012;     //changes the year
    lastWriteTime.wHour = 9;        //changes the hour
    lastWriteTime.wMinute = 37;     //changes the minute
    lastWriteTime.wSecond = 23;     //changes the second

    SYSTEMTIME lastAccessTime;
    GetSystemTime(&lastAccessTime);
    lastAccessTime.wDay = 20;       //changes the day
    lastAccessTime.wMonth = 07;     //changes the month
    lastAccessTime.wYear = 2017;    //changes the year
    lastAccessTime.wHour = 15;      //changes the hour
    lastAccessTime.wMinute = 31;    //changes the minute
    lastAccessTime.wSecond = 8;     //changes the second

    //creation of a filetimestruct and convert our new systemtime
    FILETIME lastWriteFiletime;
    SystemTimeToFileTime(&lastWriteTime, &lastWriteFiletime);

    FILETIME createFiletime;
    SystemTimeToFileTime(&createTime, &createFiletime);

    FILETIME lastAccessFileTime;
    SystemTimeToFileTime(&lastAccessTime, &lastAccessFileTime);

    //getthe handle to the file
    HANDLE filename = CreateFile("C:\\Users\\Lily\\Desktop\\varuntest.txt", FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    //set the filetime on the file
    SetFileTime(filename, &createFiletime, &lastAccessFileTime, &lastWriteFiletime);

    //close our handle.
    CloseHandle(filename);

    return 0;
}

這裡寫圖片描述
示例程式碼建立的檔案
這裡寫圖片描述
將檔案路徑修改為指向已經存在的檔案,得到的檔案時間引數(其中人為訪問過一次)

上述程式碼編譯會提示錯誤

e:\shizhenwei\vs2013projects\testsetfiletime\testsetfiletime\main.cpp(50): error C2664: “HANDLE CreateFileW(LPCWSTR,DWORD,DWORD,LPSECURITY_ATTRIBUTES,DWORD,DWORD,HANDLE)”: 無法將引數 1 從“const char [36]”轉換為“LPCWSTR”
1>          與指向的型別無關;轉換要求 reinterpret_cast、C 樣式轉換或函式樣式轉換

解決方法:
選中專案,右鍵屬性——>配置屬性——>常規——>專案預設值——>字符集,選為“使用多位元組字符集”。

總結

在win下開發,上述程式碼可直接使用。
參考