“Directory.GetLastWriteTime()”方法返回相同的日期/时间,尽管目录内的文件已更改

问题描述 投票:0回答:1

使用

File.GetLastWriteTime()
方法获取指定目录的最后写入日期和时间时,我发现一个奇怪的问题。

考虑以下代码:

// ‘path’ is directory for which need to obtain modification date/time information.
DateTime dtp = File.GetLastWriteTime(path);
DateTime dtf = File.GetLastWriteTime(path+@"\log.txt");

Console.WriteLine($"{path}, dir: {dtp:hh:mm:ss.fff}, file: {dtf:hh:mm:ss.fff}");

现在,当我更新

log.txt
文件时,例如,通过执行

echo some-text >> "log.txt"

在命令提示符下,

dtf
dtp
更新,未更改。

仅当我创建new文件或delete目录内的某些文件时,指定的目录日期和时间才会更改。

任何人都可以帮助我理解我在这里错过了什么吗?


注意事项:

  1. 使用

    Directory.GetLastWriteTime()
    方法时也会发生同样的情况。

  2. 使用Windows 10。

  3. 操作系统没有应用任何注册表修复,如本文所述: File.GetLastWriteTime 似乎返回“过时”值

c# .net-core ntfs
1个回答
0
投票

我在doc中找到的解释是:

This method may return an inaccurate value, because it uses native functions whose 
values may not be continuously updated by the operating system.

我认为这是一项挑战,因为文件或目录实际上可能是虚拟的,也可能是符号链接(在 Linux 中)。

© www.soinside.com 2019 - 2024. All rights reserved.