仅使用 C 在 windows 中修改文件创建时间

Modify file create time in windows using only C

我将文件从一个文件夹移动到 windows 中的另一个文件夹,并希望将原始文件日期戳记保存到新文件中。我可以使用 成功复制访问日期和修改日期 我还想在适用的情况下复制创建日期。我看到 C++ 具有 GetFileTime 函数 (fileapi.h) 来执行此操作,其他语言也可以执行此操作。可以纯粹用 C 来完成吗?如果可以,怎么做?

Windows API 设计为从 C 中调用。写

#include <windows.h>

在 C 程序中,编译器将接受 GetFileTimeSetFileTime 的用法。然后您需要 link 使用 kernel32.lib 导入库,默认情况下它可能已经在您的库列表中。

C++ 实际上必须使用 extern "C" 来调用这些函数,因为它们根本不是 C++ 函数(头文件会根据 #if __cplusplus 自动执行此操作,因此您不必不用担心。头文件还会自动使用 x86 上的 stdcall 调用约定标记这些声明,除非您形成函数指针,否则您也不必担心。)

这适用于我的 Window 10 系统。感谢您的帮助。

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

int main()
{

    HANDLE hFile1, hFile2;
    FILETIME ftCreate1, ftAccess1, ftWrite1, ftCreate2, ftAccess2, ftWrite2;
    SYSTEMTIME st, stUTC, stLocal;

    // file names, change accordingly
    char fname1[ ] = "C:\PathToFile\testfile01.txt";
    char fname2[ ] = "C:\PathToFile\testfile02.txt";

    BOOL bRet = FALSE;
    
    //OPEN FILES
    // open first 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
            );
    // open second file
    hFile2 = CreateFile(
                fname2,                 // file to open
                GENERIC_WRITE,          // open for writing 
                FILE_SHARE_READ,        
                NULL,                   
                OPEN_EXISTING,          
                FILE_ATTRIBUTE_NORMAL,  
                NULL                    
            );                  

    // check file handles have opened successfully
    if(hFile1 == INVALID_HANDLE_VALUE){
        printf("Could not open %s file, error %d\n", fname1, GetLastError());
        return 1;
    }
    if(hFile2 == INVALID_HANDLE_VALUE){
        printf("Could not open %s file, error %d\n", fname2, GetLastError());
        return 1;
    }
    
    // GET FILE TIMES 
    // retrieve the file times from the first file.
    if(!GetFileTime(hFile1, &ftCreate1, &ftAccess1, &ftWrite1)){
        printf("GetFileTime Failed!\n");
        return 1;
    }

    // SYSTEM TIME
    // get system time and assign to variable for saving current time - if required
    GetSystemTime(&st);                                     // gets current time
    SystemTimeToFileTime(&st, &ftAccess2);                  // converts to file time 
    
    // SET TIME ON FILE!!!
    // set file times for second file, NULL to keep existing timestamp
    if(SetFileTime(hFile2, &ftCreate1, &ftAccess2, NULL)){
        printf("SetFileTime Successful!\n");
    }else{
        printf("SetFileTime Failed!\n");
        return 1;
    }

    // PRINT
    // convert filetime to readable date format for printing to stdout
    FileTimeToSystemTime(&ftCreate1, &stUTC);
    SystemTimeToTzSpecificLocalTime(NULL, &stUTC, &stLocal);
    printf("Created: %02d/%02d/%d %02d:%02d\n", stLocal.wDay, stLocal.wMonth, stLocal.wYear, stLocal.wHour, stLocal.wMinute);   

    
    // close the file handles
    CloseHandle(hFile1);
    CloseHandle(hFile2);
    return 0;

}