Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs
) Handles button1.Click
' Note C:\\ is required to stackoverflow.com formatting '
Dim foundFiles = SearchPath("C:\\", ".eml")
Dim recentFiles = From file In foundFiles
Order By File.LastWriteTime Descending
Take 10
lstFiles.Items.Clear()
For Each file In recentFiles
lstFiles.Items.Add(file.FullName)
Next
End Sub
Function SearchPath(ByVal path As String,
ByVal ext As String
) As List(Of FileInfo)
Dim dirParent = New IO.DirectoryInfo(path)
Dim foundFiles = New List(Of FileInfo)
foundFiles.AddRange(dirParent.GetFiles("*" & ext))
For Each directory In dirParent.GetDirectories()
Try
foundFiles.AddRange(SearchPath(directory.FullName, ext))
Catch ex As System.UnauthorizedAccessException
' Ignore these exceptions. '
End Try
Next
Return foundFiles
End Function
Dim fi As New System.IO.DirectoryInfo("C:\WorkFolder")
Dim files = fi.GetFiles.ToList
Dim first = (From file In files Select file Order By file.CreationTime Ascending).FirstOrDefault
Dim last = (From file In files Select file Order By file.CreationTime Descending).FirstOrDefault