c# - File.GetLastWriteTime 似乎返回 'out of date' 值

标签 c# file-io windows-server-2008

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

DateTime logFileEnd = File.GetLastWriteTime(matchingActiveLogFile);

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

当我查看相关文件的“修改日期”时,它也是“过时”的,文件中的日志记录比“修改日期”更新。

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

最佳答案

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

过了一会儿,我们找到了这个链接:Disabling Last Access Time in Windows Vista to improve NTFS performance

An observant Windows Vista user noticed a registry named NtfsDisableLastAccessUpdate under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\ControlFileSystem and asked us what this means.

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.

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 behavior set disablelastaccess 0

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",或者在该文件中一些领域。

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

.NET FileInfo.LastWriteTime & FileInfo.LastAccessTime are wrong ,也可能对您有用。

我对此的一般看法是:不要依赖那个参数,而是在你的架构中发明其他东西。

祝你好运

关于c# - File.GetLastWriteTime 似乎返回 'out of date' 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9992223/

相关文章:

php - 如何使用PHP File API写入原始字节?

c# - 以列表的形式访问所有可用的 ViewComponents

c# - 如何使用 Observables 构建限速 API?

java - 如何从 USB 内存棒中删除文件?使用 File.delete() 不起作用

C++ 新手 : writing a function for extracting i vectors from a file. 如何从函数中获取未知数量的 vector ?

c# - DispatcherTimer 在 Win 2k8 SP2 中的 Stop() 后继续触发

c# - Moq:单元测试依赖于 HttpContext 的方法

c# - WCF Restful POST 从服务器到客户端返回错误消息

sql-server - MSDTC 的问题 - 远程事务

powershell - 如何使用 Powershell 使用 SAML 1.1 配置文件设置 AD FS 2.0?