C++ 可执行安装时间

C++ 可执行安装时间,c++,windows,timestamp,exe,C++,Windows,Timestamp,Exe,我有一个安装在计算机上的windows可执行文件。是否有一种方法或API可以在可执行文件安装到该计算机上时获取时间戳。我不是问exe的创建/修改/访问时间戳,而是问exe安装在特定机器上的时间。 此外,exe已安装在windows系统文件夹中。 您可以使用FileTimeToSystemTime()检索创建文件或目录的日期和时间 #include <windows.h> #include <stdio.h> int main(){ // a file hand

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

您可以使用
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;
}
#包括
#包括
int main(){
//文件句柄
处理hFile1;
文件时间ftCreate、ftAccess、ftWrite;
系统时间stUTC、stLocal、stUTC1、stLocal1、stUTC2、stLocal2;
//文件名,
char fname1[]=“c:\\windows\\explorer.exe”;
//文件大小的临时存储
DWORD文件大小;
DWORD dwFileType;
//打开现有文件
hFile1=CreateFile(fname1,//要打开的文件
GENERIC_READ,//打开进行读取
文件\u共享\u读取,//共享以进行读取
NULL,//默认安全性
打开现有文件,//仅打开现有文件
文件\属性\正常,//正常文件
NULL);//没有属性模板
if(hFile1==无效的句柄值){
printf(“无法打开%s文件,错误%d\n”,fname1,GetLastError());
返回4;
}
dwFileType=GetFileType(hFile1);
dwFileSize=GetFileSize(hFile1,NULL);
printf(“%s大小为%d字节,文件类型为%d\n”,fname1,dwFileSize,dwFileType);
//检索文件的文件时间。
if(!GetFileTime(hFile1、&ftCreate、&ftAccess、&ftWrite)){
printf(“有什么不对劲的lol!\n”);
返回FALSE;
}
//将创建的时间转换为本地时间。
FileTimeToSystemTime(&ftCreate,&stUTC);
SystemTimeToTzSpecificClocalTime(NULL、stUTC和stLocal);
//将上次访问时间转换为本地时间。
FileTimeToSystemTime(&ftAccess,&stUTC1);
SystemTimeToTzSpecificClocalTime(NULL、&stUTC1、&stLocal1);
//将上次写入时间转换为本地时间。
FileTimeToSystemTime(&ftWrite,&stUTC2);
SystemTimeToTzSpecificClocalTime(NULL、stUTC2和stLocal2);
//构建一个显示日期和时间的字符串。
printf(“\n创建日期:%02d/%02d/%d%02d:%02d\n”,stLocal.wDay,stLocal.wMonth,stLocal.wYear,stLocal.wHour,stLocal.wMinute);
printf(“上次访问:%02d/%02d/%d%02d:%02d\n”,stLocal1.wDay,stLocal1.wMonth,stLocal1.wYear,stLocal1.wHour,stLocal1.wMinute);
printf(“上次写入:%02d/%02d/%d%02d:%02d\n\n”,stLocal2.wDay,stLocal2.wMonth,stLocal2.wYear,stLocal2.wHour,stLocal2.wMinute);
//关闭文件句柄和文件本身
闭合手柄(hFile1);
返回0;
}

注册表中可能有一个条目。定义“已安装”。安装时间是指在计算机上复制exe的时间。
我不是问该exe的创建/修改/访问时间戳,
安装时间是指在计算机上复制exe的时间。
?这是一个矛盾。@user3364310“是”与Windows上的普通文件副本,“可能不是”,如果它是由某个存档者等提取的(这类程序通常具有保留时间戳的可选设置)我的印象是创建时间是文件最初在主生成计算机上创建的时间,但看起来好像我在X时间生成了一个exe,但在M计算机上复制了Y时间的exe,那么M计算机上文件的创建时间将是Y时间。是否正确?是的,如果将exe复制到另一个位置,它将获得新的时间戳。我已经更新了代码。它显示创建日期、上次访问的时间和修改的时间。