Dim dir As String = "C:\Parametre"
Dim latestFilename As String = ""
Dim latestDatetime As DateTime = DateTime.MinValue
Dim di As IO.DirectoryInfo = New System.IO.DirectoryInfo(dir)
For Each fi As IO.FileInfo In di.GetFiles()
If fi.CreationTimeUtc > latestDatetime Then
latestDatetime = fi.CreationTimeUtc
latestFilename = fi.FullName
End If
Next
' The variable latestFilename now contains the full name of
' the file with the latest creation date in the directory dir.
1条答案
按热度按时间ruoxqz4g1#
你可以创建一个变量来保存找到的最新日期,并将其初始化为可能的最小值。然后你迭代所有文件,看看每个文件的创建日期是否晚于你存储的日期,如果是,那么你更新最新日期并记住文件名,如下所示:
(理论上,虽然不太可能,但可能有多个文件具有相同的最新创建时间戳,上面的代码将仅返回文件系统中记录的第一个文件。