File.GetLastWriteTime 似乎正在返回“过时"值 [英] File.GetLastWriteTime seems to be returning 'out of date' value

查看:31
本文介绍了File.GetLastWriteTime 似乎正在返回“过时"值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个工具来收集工具用户指定的时间窗口内的日志文件.到目前为止,我是通过在日志文件上使用 File.GetLastWriteTime 方法来收集日志文件,将其与用户输入的时间进行比较,并根据这些比较的结果进行收集.这是一个小代码片段:

I have written a tool to collect log files within a time window specified by the user of the tool. Up until now I was basing the collection of log files by using the File.GetLastWriteTime method on the log files, comparing this to the times the user entered and collecting based on the outcome of these comparisions. Here is a small code snippet:

DateTime logFileEnd = File.GetLastWriteTime(matchingActiveLogFile);

但是我注意到我的工具没有收集一些我认为应该收集的日志文件.此方法返回的 DateTime 似乎已过期,(文件中的日志记录比此日期时间的值要多).

However I noticed my tool didnt collect some log files I thought it should have done. It seems the DateTime returned by this method was out of date, (there was more recent logging in the file than the value of this datetime).

当我查看相关文件的修改日期"时,它也过时",文件中的日志记录比修改日期"要多.

When I looked at the 'Date Modified' of the file in question, it too was 'out of date', there was more recent logging in the file than the 'Date Modified'.

如何获得准确的GetLastWriteTime"或修改日期"值?

How I can I get an accurate 'GetLastWriteTime' or Date Modified value?

推荐答案

在我的经历中,我抛出了几个像你一样的问题.在运行并不总是的 Windows Vista/7 系统上返回可靠的结果.

During my experience I went throw a couple of issues like yours. On Windows Vista/7 systems that function not always returns a reliable result.

过了一会儿,我们找到了这个链接:在 Windows Vista 中禁用上次访问时间以提高 NTFS 性能

After a while we found this link: Disabling Last Access Time in Windows Vista to improve NTFS performance

一位细心的 Windows Vista 用户注意到一个名为NtfsDisableLastAccessUpdateHKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlFileSystem 并询问我们这是什么意思.

An observant Windows Vista user noticed a registry named NtfsDisableLastAccessUpdate under HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlFileSystem and asked us what this means.

Last Access Time 是一个文件属性,它是在访问或以其他方式触摸文件时更新.(这经常与 Last Modified Time 混淆,后者仅在文件更改.)上次访问时间有一个松散的粒度,只有保证时间精确到一小时以内.

Last Access Time is a file attribute that’s updated when a file is accessed or otherwise touched. (This is often confused with the Last Modified Time, which is only updated when the file changes.) Last Access Time has a loose granularity that only guarantees that the time is accurate to within one hour.

在 Windows 中Vista,我们已禁用对上次访问时间的更新以改进 NTFS表现.如果您使用的应用程序依赖于此值,您可以使用以下命令启用它:

In Windows Vista, we’ve disabled updates to Last Access Time to improve NTFS performance. If you are using an application that relies on this value, you can enable it using the following command:

fsutil 行为设置 disablelastaccess 0

您必须重新启动计算机才能使此更改生效.有关 Fsutil 命令和上次访问时间的更多信息,请参阅 Fsutil 在线帮助.

You must restart the computer for this change to take effect. For more information about the Fsutil command and Last Access Time, see the Fsutil online Help.

基于此,很明显上次访问时间不能用作强键".为了解决这个问题,我们只是停止在 GetLastWriteTime 调用上进行中继,而是存储文件的最后更改值或其名称,如 "FileName_yyyymmdd",或在该文件中某个领域.

Based on this it became clear that last access time can not be used as a "strong key". To resolve that issue, we just stop relaying on GetLastWriteTime call, but store last changed value of the file or in its name, like "FileName_yyyymmdd", or inside that file in some field.

GetLastAccessTime 的另一个解决方案可以在这里找到:

There is another solution for GetLastAccessTime can find here:

.NET FileInfo.LastWriteTime &FileInfo.LastAccessTime 是错误的,对您的情况也很有用.

.NET FileInfo.LastWriteTime & FileInfo.LastAccessTime are wrong, could be useful in your case too.

我对此的总体看法是:不要依赖该参数,而是在您的架构中发明其他东西.

My general opinion on this would be: do not relay on that parameter, but invent something else in your architecture.

祝你好运

这篇关于File.GetLastWriteTime 似乎正在返回“过时"值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆