我有一段代码,该代码块读取目录中的所有文件,并从所有这些文件中获取最新日期。不知道我是不是很傻,但是是否可以通过获取该目录中文件夹而不是文件的上次修改日期来做相同的事情?
Dim chkPath = "C:\CheckFolders"
Dim directory As New System.IO.DirectoryInfo(chkPath)
Dim File As System.IO.FileInfo() = directory.GetFiles()
Dim File1 As System.IO.FileInfo
Dim LastModified As String
For Each File1 In File
LastModified = System.IO.File.GetLastWriteTime(chkPath & "\" & File1.Name).ToShortDateString()
Next
MsgBox(LastModified)
答案 0 :(得分:1)
您应该主动找到最新的目录,而不是希望目录条目按升序返回。您可以使用LINQ Max方法来做到这一点:
Shared Function GetLatestFileModified(d As String) As DateTime
Dim di = New DirectoryInfo(d)
Dim latest = di.EnumerateFiles().Max(Function(i) i.LastWriteTimeUtc)
Return latest
End Function
Shared Function GetLatestDirectoryModified(d As String) As DateTime
Dim di = New DirectoryInfo(d)
Dim latest = di.EnumerateDirectories().Max(Function(i) i.LastWriteTimeUtc)
Return latest
End Function
例如,
Dim src = "C:\temp"
Console.WriteLine(GetLatestFileModified(src).ToShortDateString())
Console.WriteLine(GetLatestDirectoryModified(src).ToShortDateString())
可能给予
26/04/2019
10/04/2019
答案 1 :(得分:0)
我的解决方案中包含以下代码。
'Check if file needs updating
Dim infoStkReader As System.IO.DirectoryInfo
infoStkReader = My.Computer.FileSystem.GetDirectoryInfo(SUI)
Dim CurrentdirectoryDate As DateTime = infoStkReader.LastWriteTime
其中SUI是目录路径。
谢谢, 理查德。