2015-07-11 115 views
-4

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

+0

在注册表中可能有一个条目。 –

+7

定义“已安装”。 –

+0

安装时间表示何时将exe复制到机器上。 – user3364310

回答

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; 
} 
+0

我的印象是,创建时间是文件最初在主构建机器上创建的时间,但看起来像是我在X时间构建了一个exe文件,但是在Y时间在M机器上复制了这个exe文件,然后创建了时间M机器上的文件将是Y时间。它是否正确? – user3364310

+0

是的,如果你将exe复制到另一个位置,那么它会得到一个新的时间戳。我更新了代码。它显示了创建日期,上次访问时间和修改时间。 –

+0

完美。谢谢 。 – user3364310