简体   繁体   English

上次写入FILETIME总是返回当前时间

[英]Last write FILETIME always returning current time

I need to compare a file's last modified time to a date time stored in a database. 我需要将文件的最后修改时间与存储在数据库中的日期时间进行比较。 I initially looked at this question to get started. 最初,我着眼于这个问题

I am currently getting the FILETIME for the last write of the file, converting it to a SYSTEMTIME . 我目前正在获取文件最后一次写入的FILETIME ,并将其转换为SYSTEMTIME Then I use that SYSTEMTIME to create a TDateTime object that I can use for my comparison. 然后,我使用该SYSTEMTIME创建一个可用于比较的TDateTime对象。 However, the FileModifiedDT variable, is always coming out to be the current time, despite the file having been modified previously. 但是,尽管文件先前已被修改,但FileModifiedDT变量始终显示为当前时间。

FILETIME lastWriteTime;

String * FileNamePtr = new String( FileName );

GetFileTime( FileNamePtr, NULL, NULL, &lastWriteTime );

SYSTEMTIME systemTime;
FileTimeToSystemTime( &lastWriteTime, &systemTime );

TDateTime * FileModifiedDT = new TDateTime( systemTime.wYear, systemTime.wMonth,
                                            systemTime.wDay, systemTime.wHour,
                                            systemTime.wMinute, systemTime.wSecond,
                                            systemTime.wMilliseconds );

Am I missusing GetFileTime in some way? 我会以某种方式滥用GetFileTime吗? Is there a better way I should go about this? 我应该有更好的方法吗?

The error is 错误是

String * FileNamePtr = new String( FileName );
GetFileTime( FileNamePtr, NULL, NULL, &lastWriteTime );

According to the documentation , the first argument has to be a file handle created by CreateFile and nothing else. 根据文档 ,第一个参数必须是CreateFile创建的文件句柄,而没有别的。

Thus you need something like this: 因此,您需要这样的东西:

HANDLE fileHandle = CreateFile(
  FileName, //LPCTSTR
  GENERIC_READ,
  FILE_SHARE_READ,
  NULL,
  OPEN_EXISTING,
  FILE_ATTRIBUTE_NORMAL,
  NULL
);

if ( fileHandle != INVALID_HANDLE )
{
    GetFileTime( fileHandle, NULL, NULL, &lastWriteTime );
    CloseHandle( fileHandle );
}
else
{
    // error, do something else...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM