If you want to get newest file from any specific directory, you can use the following source code:
C# code
FileInfo GetNewestFile(DirectoryInfo directoryInfo)
{
return directoryInfo.GetFiles()
.Union(directoryInfo.GetDirectories().Select(d => GetNewestFile(d)))
.OrderByDescending(f => (f == null ? DateTime.MinValue : f.LastWriteTime))
.FirstOrDefault();
}
VB.Net code
Function GetNewestFile(ByVal directoryInfo As DirectoryInfo) As FileInfo
Return directoryInfo.GetFiles()
.Union(directoryInfo.GetDirectories().Select(d => GetNewestFile(d)))
.OrderByDescending(f => (f = IIf(Nothing, DateTime.MinValue, f.LastWriteTime)))
.FirstOrDefault()
End Function
Happy Coding!!
0 Comment(s)