windows 如何以编程方式设置文件的修改时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2185338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:52:41  来源:igfitidea点击:

How to set the modification time of a file programmatically?

cwindowsfile

提问by Jay

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

如何在 Windows 中以编程方式设置文件的修改时间?

回答by DVK

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

来自: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;
}

回答by ephemient

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

Windows(或标准 CRT,无论如何)具有与UNIX相同的utimes系列函数。

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

Using Win32 functions, FILE_BASIC_INFOcan be set using SetFileInformationByHandle.

使用Win32函数,FILE_BASIC_INFO可以使用设置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));

回答by user151019

Use SetFileInformationByHandlewith FileInformationType as FILE_BASIC_INFO

使用SetFileInformationByHandle和 FileInformationType 作为 FILE_BASIC_INFO

回答by GingerHyman

I found this to be useful on windows SetFileTime()

我发现这在 Windows SetFileTime()上很有用