JerryT
JerryT

Reputation: 128

VB.Net- How to get most recently created files in folder/How to get 10 most recent files from folder

I searched for 2 days trying to find the answer for how to do this in VB.net. I found multiple answers for C#, but the syntax was off and I couldn't fixture it out, until I found this which worked flawlessly.

Upvotes: 1

Views: 731

Answers (1)

JerryT
JerryT

Reputation: 128

'Newest first, oldest last
Dim filesByCreateDate = New DirectoryInfo("Folder to Search").GetFiles().OrderByDescending(Function(fi) fi.CreationTime).Take(10).ToArray

'Newest File
Dim newestFile = New DirectoryInfo("Folder to Search").GetFiles().OrderByDescending(Function(fi) fi.CreationTime).First

'Oldest File
Dim oldestFile = New DirectoryInfo("Folder to Search").GetFiles().OrderBy(Function(fi) fi.CreationTime).First

Upvotes: 1

Related Questions