问题描述
上下文:
问题:
使用FilecopyExW
,我已成功将文件复制到新位置,但是文件的日期和时间不匹配,可能会延迟一秒钟或更长时间,如下例所示。
尝试用源文件的名称(有时)修改覆盖目标文件的QDateTime
不能正确写入日期,如下面的示例所示。
调试器的屏幕截图
09:34:50.731 Warning: File times do not match
09:34:50.731 Warning: Birth time differene: 0
09:34:50.731 Warning: Last Access time differene: 483
09:42:02.660 Warning: Last Modified time differene: 483
09:42:02.660 Debug:
09:43:48.831 Warning: File times do not match
09:43:48.831 Warning: Birth time differene: -7
09:43:48.831 Warning: Last Access time differene: 1462
09:44:53.602 Warning: Last Modified time differene: 0
09:44:53.602 Debug:
有什么想法会发生这种情况以及如何解决?
代码示例:
// File copied successfully at this point,Now adjust file times as these may not be accurate (why?)
QFile newFile(destination);
if (newFile.open(qiodevice::OpenModeFlag::writeonly | qiodevice::OpenModeFlag::Append)) {
// set file attributes,create,last write and last modified dates the same as original file
QFileInfo srcFi(storageFile.location);
// Set last modified date - most important
QDateTime localLastModified = srcFi.lastModified().toLocalTime();
if (!newFile.setFileTime(localLastModified,QFileDevice::FileModificationTime)) {
printWarningFileError(TAG,QString("Failed to set lastModified date for file at [%1]").arg(destination),&newFile);
}
// Set last accessed date
QDateTime localLastRead = srcFi.lastRead().toLocalTime();
if (!newFile.setFileTime(localLastRead,QFileDevice::FileAccesstime)) {
printWarningFileError(TAG,QString("Failed to set lastAccessed date for file at [%1]").arg(destination),&newFile);
}
// Set created date
QDateTime localBirthTime = srcFi.birthTime().toLocalTime();
if (!newFile.setFileTime(localBirthTime,QFileDevice::FileBirthTime)) {
printWarningFileError(TAG,QString("Failed to set created date for file at [%1]").arg(destination),&newFile);
}
// Ensure we write out content before closing file (not really necessary due to close() )
newFile.flush();
newFile.close();
// Read file & refresh Meta data for checking
QFileInfo fiDest(destination);
fiDest.refresh();
// Read destination file date data
QDateTime destFileBirth = fiDest.birthTime();
QDateTime destFileAccess = fiDest.lastRead();
QDateTime destFileModified = fiDest.lastModified();
// Compare dates
bool birthTimeMatch = (destFileBirth != localBirthTime);
bool accesstimeMatch = (destFileAccess != localLastRead);
bool modifiedTimeMatch = (destFileModified != localLastModified);
if (!(birthTimeMatch && accesstimeMatch && modifiedTimeMatch)) {
qWarning() << "File times do not match";
qWarning() << "Birth time differene: " << (destFileBirth.toMSecsSinceEpoch() - localBirthTime.toMSecsSinceEpoch());
qWarning() << "Last Access time differene: " << (destFileAccess.toMSecsSinceEpoch() - localLastRead.toMSecsSinceEpoch());
qWarning() << "Last Modified time differene: " << (destFileModified.toMSecsSinceEpoch() - localLastModified.toMSecsSinceEpoch());
qDebug() << "";
}
else {
qDebug() << "All good";
}
}
else {
printWarningFileError(TAG,QString("Failed to open file for adjust dates [%1]").arg(destination),&newFile);
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)