How to set the modification time of a file program

2020-03-02 04:42发布

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

标签: c windows file
5条回答
疯言疯语
2楼-- · 2020-03-02 04:47

From: http://rosettacode.org/wiki/File/Modification_Time#C

#include <time.h>
#include <utime.h>
#include <sys/stat.h>

const char *filename = "input.txt";

int main() {
  struct stat foo;
  time_t mtime;
  struct utimbuf new_times;

  stat(filename, &foo);
  mtime = foo.st_mtime; /* seconds since the epoch */

  new_times.actime = foo.st_atime; /* keep atime unchanged */
  new_times.modtime = time(NULL);    /* set mtime to current time */
  utime(filename, &new_times);

  return 0;
}
查看更多
叼着烟拽天下
4楼-- · 2020-03-02 04:58

I found this to be useful on windows SetFileTime()

查看更多
啃猪蹄的小仙女
5楼-- · 2020-03-02 05:05

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));
查看更多
啃猪蹄的小仙女
6楼-- · 2020-03-02 05:10

Use SetFileInformationByHandle with FileInformationType as FILE_BASIC_INFO

查看更多
登录 后发表回答