How to set the modification time of a file programmatically?

后端 未结 5 689
余生分开走
余生分开走 2021-01-05 07:48

How do I set the modification time of a file programmatically in Windows?

5条回答
  •  自闭症患者
    2021-01-05 08:32

    Windows (or the standard CRT, anyhow) has the same utimes family of functions that UNIX has.

    struct _utimebuf t;
    t.tma = 1265140799;  // party like it's 1999
    t.tmm = 1265140799;
    _utime(fn, &t);
    

    Using Win32 functions, FILE_BASIC_INFO can be set using SetFileInformationByHandle.

    FILE_BASIC_INFO b;
    b.CreationTime.QuadPart = 1265140799;
    b.LastAccessTime.QuadPart = 1265140799;
    b.LastWriteTime.QuadPart = 1265140799;
    b.ChangeTime.QuadPart = 1265140799;
    b.FileAttributes = GetFileAttributes(fn);
    SetFileInformationByHandle(h, FileBasicInfo, &b, sizeof(b));
    

提交回复
热议问题