- 1 year ago
how to know a file's create time by others
Comment
1 / 1
On Windows, you can use the GetFileTime
function from the Windows API to retrieve a file's creation time programmatically, even if the file was created by someone else. Here's how you can do it in C++:
cpp#include <windows.h>
#include <stdio.h>
int main()
{
const char* filename = "C:\\path\\to\\your\\file.txt"; // Replace with the actual file path
FILETIME createFileTime, accessFileTime, writeTime;
HANDLE hFile = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
if (GetFileTime(hFile, &createFileTime, &accessFileTime, &writeTime))
{
// Convert FILETIME to a readable time format
SYSTEMTIME systemTime;
FILETIME localTime;
FileTimeToLocalFileTime(&createFileTime, &localTime);
FileTimeToSystemTime(&localTime, &systemTime);
// Print the creation time
printf("File Creation Time: %04d-%02d-%02d %02d:%02d:%02d\n",
systemTime.wYear, systemTime.wMonth, systemTime.wDay,
systemTime.wHour, systemTime.wMinute, systemTime.wSecond);
}
else
{
printf("Failed to get file time. Error code: %d\n", GetLastError());
}
CloseHandle(hFile);
}
else
{
printf("Failed to open file. Error code: %d\n", GetLastError());
}
return 0;
}
Replace "C:\\path\\to\\your\\file.txt"
with the actual path of the file you want to check. The program will open the file, retrieve its creation time, convert it to a readable format, and print it to the console.
Please note that to use the Windows API functions, you need to compile the code as a Windows application (e.g., using Visual Studio). If you are using a different programming language or platform, similar functions might be available to access a file's creation time.