在VB.NET中获取文件修改日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3668340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:43:12  来源:igfitidea点击:

Get file modified date in VB.NET

.netvb.netfile-iolast-modified

提问by Ianb

I have a number of files in a folder, and I need to get the last modified date. So I used

我在一个文件夹中有许多文件,我需要获取上次修改日期。所以我用

FDate = IO.File.GetLastWriteTime(FName)

It works fine with some files, but on others, I get a date of 1/1/1601. But when I check the files in Windows Explorer, all the dates look normal (recent). So, I'm guessing there are multiple file dates stored in the file system, and the ones .NET is seeing are not the ones Windows is seeing. How can I get exactly the date which appears as "date modified" in a file explorer window?

它适用于某些文件,但在其他文件中,我得到的日期为 1/1/1601。但是当我在 Windows 资源管理器中检查文件时,所有日期看起来都很正常(最近)。所以,我猜在文件系统中存储了多个文件日期,而 .NET 看到的不是 Windows 看到的。如何准确获取在文件资源管理器窗口中显示为“修改日期”的日期?

I tried some Visual Basic 6.0API stuff, but that doesn't seem to work in .NET.

我尝试了一些Visual Basic 6.0API 的东西,但这在 .NET 中似乎不起作用。

回答by Martin Liversage

From File.GetLastWriteTime Method:

File.GetLastWriteTime 方法

If the file described in the path parameter does not exist, this method returns 12:00 midnight, January 1, 1601 A.D. (C.E.) Coordinated Universal Time (UTC), adjusted to local time.

如果路径参数中描述的文件不存在,则此方法返回 1601 年 1 月 1 日午夜 12:00 AD (CE) 协调世界时 (UTC),调整为本地时间。

The file you are querying is probably missing.

您正在查询的文件可能丢失了。

回答by Praveen

The query mentioned below will get the correct LastModifiedDatefor all of the files contained in a folder.

下面提到的查询将正确LastModifiedDate获取文件夹中包含的所有文件。

    Dim strFilepath = ""  'Specify path details
    Dim directory As New System.IO.DirectoryInfo(strFilepath)
    Dim File As System.IO.FileInfo() = directory.GetFiles()
    Dim File1 As System.IO.FileInfo
    For Each File1 In File
        Dim strLastModified As String
        strLastModified = System.IO.File.GetLastWriteTime(strFilepath & "\" & File1.Name).ToShortDateString()
    Next