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 files 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.0 API stuff, but that doesn't seem to work in .NET.
我尝试了一些Visual Basic 6.0 API的东西,但这似乎不适用于.NET。
2 个解决方案
#1
29
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.
如果path参数中描述的文件不存在,则此方法返回公元1601年1月1日午夜12点(美国)协调世界时(UTC),调整为当地时间。
The file you are querying is probably missing.
您查询的文件可能已丢失。
#2
4
The query mentioned below will get the correct LastModifiedDate
for 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
#1
29
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.
如果path参数中描述的文件不存在,则此方法返回公元1601年1月1日午夜12点(美国)协调世界时(UTC),调整为当地时间。
The file you are querying is probably missing.
您查询的文件可能已丢失。
#2
4
The query mentioned below will get the correct LastModifiedDate
for 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