gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:08:13 25 4
gpt4 key购买 nike

我编写了一个工具,用于在工具用户指定的时间窗口内收集日志文件。到目前为止,我是通过使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com