【问题标题】:Windows filesystem: Creation time of a file doesn't change when while is deleted and created againWindows文件系统:删除并再次创建文件时,文件的创建时间不会改变
【发布时间】:2012-01-10 13:53:12
【问题描述】:

我有以下场景:

  • 1:创建一堆文件

  • 2:调用一些外部应用程序来处理所有不同的文件 自上次快照以来的创建时间

  • 3:删除文件

  • 4:转到 1

事实证明,当用户创建文件、删除文件以及创建同名文件时,windows 并不能保证它会更改创建时间。

我编写了一个小型 powershell 脚本来验证这一点:

ls | Remove-Item

$fileListOld = @{}
foreach($i in 1..1000)
{
    $fname = [string]::Format("{0}.txt", $i)
    "tst" >> $fname    
}

ls | % { $fileListOld[$_.Name] = $_ }
ls | Remove-Item

foreach($i in 1..1000)
{
    $fname = [string]::Format("{0}.txt", $i)
    "tst" >> $fname    
}

$fileListNew = @{}
ls | % { $fileListNew[$_.Name] = $_ }

$count = 0



foreach ($fname in $fileListNew.Keys)
{
    if ($fileListNew[$fname].CreationTimeUtc -eq $fileListOld[$fname].CreationTimeUtc)
    {
        Write-Host Same creation time -ForegroundColor Red
        Write-Host $fname -ForegroundColor Red
        $count++
    }
}

Write-Host $count

输出:

...
...
Same creation time
241.txt
Same creation time
944.txt
Same creation time
908.txt
Same creation time
631.txt
Same creation time
175.txt
Same creation time
798.txt
Same creation time
192.txt
Same creation time
961.txt
Same creation time
476.txt
Same creation time
566.txt
Same creation time
945.txt
Same creation time
681.txt
Same creation time
880.txt
Same creation time
162.txt
Same creation time
634.txt
Same creation time
746.txt
Same creation time
442.txt
Same creation time
35.txt
Same creation time
96.txt
Same creation time
771.txt
Same creation time
787.txt
Same creation time
972.txt
Same creation time
642.txt
Same creation time
495.txt
Same creation time
625.txt
Same creation time
666.txt
Same creation time
660.txt
Same creation time
6.txt
Same creation time
593.txt
Same creation time
549.txt
Same creation time
842.txt
Same creation time
314.txt
Same creation time
148.txt
**1000**

如果我在删除后睡了一段时间(>30 秒),所有文件都会有正确的时间戳。

有没有办法解决这个问题?某些 winapi 调用会永久删除文件?

【问题讨论】:

  • 这被称为“隧道”。有关详细信息,请参阅this KB article。 (该文件被永久删除。它只是创建时间和其他元数据正在传递。)
  • @RaymondChen,从我收集到的字里行间读到,这是为了为使用短文件名的程序保留长文件名?
  • 这是其中的一部分。但也适用于使用创建/重命名/删除技巧来替换文件的应用程序。这会将元数据从旧文件传播到新文件。

标签: windows winapi filesystems


【解决方案1】:

我相信您在 Windows 中遇到了一种称为 filesystem tunneling 的现象。这是基于 NT 的系统的一个功能,其中与同一目录中最近删除的文件同名的新文件将继承旧文件的创建时间。

您可以禁用隧道或更改缓存旧文件数据的时间长度。有关详细信息,请参阅此Microsoft KB article

文件系统隧道已实现,因为许多应用程序将删除和重新创建他们希望更改的文件,而不仅仅是更新它们。

您应该能够使用@Jim Rhodes 的建议来抵消此功能。

【讨论】:

    【解决方案2】:

    您可以在创建文件后立即使用SetFileTime 更新创建时间。

    【讨论】:

    • File.SetCreationTime 适用于所有使用 .NET 的人
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-31
    • 1970-01-01
    • 2012-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多