可执行安装时间

时间:2015-07-11 14:29:00

标签: c++ windows timestamp exe

我有一台Windows可执行文件,我已安装在一台机器上。是否有一种方法或API来获取在该计算机上安装可执行文件时的时间戳。我不是要求该exe的创建/修改/访问时间戳,而是在特定机器上安装exe的时间。 此外,exe安装在Windows系统文件夹中。

1 个答案:

答案 0 :(得分:0)

enter image description here

您可以使用FileTimeToSystemTime()来检索创建文件或目录的日期和时间。

#include <windows.h>
#include <stdio.h>

int main(){

    // a file handle
    HANDLE hFile1;
    FILETIME ftCreate, ftAccess, ftWrite;
    SYSTEMTIME stUTC, stLocal, stUTC1, stLocal1, stUTC2, stLocal2;

    // a filename,  
    char fname1[ ] = "c:\\windows\\explorer.exe";

    // temporary storage for file sizes
    DWORD dwFileSize;
    DWORD dwFileType;

    // opening the existing file
    hFile1 = CreateFile(fname1,                // file to open
                    GENERIC_READ,                // open for reading
                    FILE_SHARE_READ,         // share for reading
                    NULL,                                     // default security
                    OPEN_EXISTING,               // existing file only
                    FILE_ATTRIBUTE_NORMAL, // normal file
                    NULL);                                         // no attribute template

    if(hFile1 == INVALID_HANDLE_VALUE){
        printf("Could not open %s file, error %d\n", fname1, GetLastError());
        return 4;
    }

    dwFileType = GetFileType(hFile1);
    dwFileSize = GetFileSize(hFile1, NULL);
    printf("%s size is %d bytes and file type is %d\n", fname1, dwFileSize, dwFileType);

    // retrieve the file times for the file.
     if(!GetFileTime(hFile1, &ftCreate, &ftAccess, &ftWrite)){
      printf("Something wrong lol!\n");
             return FALSE;
    }

     // convert the created time to local time.
     FileTimeToSystemTime(&ftCreate, &stUTC);
     SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);

    // convert the last-access time to local time.
    FileTimeToSystemTime(&ftAccess, &stUTC1);
    SystemTimeToTzSpecificLocalTime(NULL, &stUTC1, &stLocal1);

    // convert the last-write time to local time.
    FileTimeToSystemTime(&ftWrite, &stUTC2);
    SystemTimeToTzSpecificLocalTime(NULL, &stUTC2, &stLocal2);

    // build a string showing the date and time.
    printf("\nCreated on: %02d/%02d/%d %02d:%02d\n", stLocal.wDay, stLocal.wMonth, stLocal.wYear, stLocal.wHour, stLocal.wMinute);
    printf("Last accessed: %02d/%02d/%d %02d:%02d\n", stLocal1.wDay, stLocal1.wMonth, stLocal1.wYear, stLocal1.wHour, stLocal1.wMinute);
    printf("Last written: %02d/%02d/%d %02d:%02d\n\n", stLocal2.wDay, stLocal2.wMonth, stLocal2.wYear, stLocal2.wHour, stLocal2.wMinute);

    // close the file's handle and itself
    CloseHandle(hFile1);
return 0;
}