【问题标题】:How can I determine a file's creation date in Windows?如何在 Windows 中确定文件的创建日期?
【发布时间】:2011-06-18 01:39:59
【问题描述】:

如何获取文件的创建日期?我正在运行 Windows。

【问题讨论】:

  • 在哪个操作系统下?
  • 科迪,windows 我猜,win98/2000/vista 和 7 的 api 没有变化
  • @Mandrake:当 Cody 发表评论时,问题中没有说 windows。

标签: c++ c windows winapi file


【解决方案1】:

在 Windows 上,您应该为此使用 GetFileAttributesEx 函数。

【讨论】:

  • 我使用这种方法是因为我想知道几个文件的创建日期,但我得到的所有文件的值相同 ftCreationTime.dwLowDateTime 和` ftCreationTime.dwLowDateTime `,为什么发生这种情况?谢谢
  • @Alexander:也许这些文件对所有文件都具有相同的值?
  • 这些文件的日期相同,但我预计会有一些差异,因为它们都有不同的时间
【解决方案2】:

对于 C,这取决于您编写的操作系统。文件是一个依赖于系统的概念。

如果您的系统有它,您可以使用stat()(和朋友)功能:http://pubs.opengroup.org/onlinepubs/009695399/functions/stat.html

在 Windows 上,您可以使用 GetFileTime() 函数:http://msdn.microsoft.com/en-us/library/ms724320%28v=vs.85%29.aspx

【讨论】:

  • GetFileAttributesEx 在此特定用途上优于 GetFileTime,原因有以下三个: 1. 使用 GetFileTime 需要打开文件的句柄(因此必须正确管理句柄) 2 . 由于 (1),性能可能会受到影响,因为系统实际上必须查找文件而不是 MFT 位置 3。由于 (1),GetFileAttributesEx 通常需要比GetFileTime 更少的权限,并且自动要求检索文件元数据而不是文件本身所需的最低权限。
【解决方案3】:

Unix 系统不存储文件创建时间。 Unix 系统do 存储上次读取文件的时间(如果为该特定文件系统打开了atime;有时为了速度而禁用它),上次修改文件的时间(@987654322 @),以及上次更改文件元数据的时间 (ctime)。

有关使用它的详细信息,请参阅stat(2) 联机帮助页。

【讨论】:

    【解决方案4】:

    使用统计功能

    here

    #include <sys/stat.h>
    
    #include <unistd.h>
    
    #include <time.h>
    
    
    
    struct tm* clock;               // create a time structure
    
    struct stat attrib;         // create a file attribute structure
    
    stat("afile.txt", &attrib);     // get the attributes of afile.txt
    
    clock = gmtime(&(attrib.st_mtime)); // Get the last modified time and put it into the time structure
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    • 2015-10-09
    • 2014-01-28
    • 2018-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多