Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following that populates a listbox but I need the files ordering by creation date/time first. Newest top of the list. Sounds easy enough but I just cant figure out how. Any help much appreciated! Thanks in advance

VB
For Each file As String In Directory.GetFiles("C:\ProgramData\Folder\Temp\", "*.pdf")

    lstPDFs.Items.Add(Path.GetFileNameWithoutExtension(file))

Next


What I have tried:

Googled but cant seem to get my head around it
Posted
Updated 27-Sep-18 11:48am
Comments
MadMyche 27-Sep-18 15:06pm    
What you are looking for is the filesysteminfo.creationtime property, and utilize in a LINQ .Sort method

How about this:

List<FileInfo> lstFiles = new List<FileInfo>();
foreach (var file in Directory.GetFiles(@"C:\yourfolder\", "*.pdf"))
{
    FileInfo fInfo = new FileInfo(file);
    lstFiles.Add(fInfo);
}

var lstPDFs = lstFiles.OrderBy(f => f.CreationTime).Select(f => f.FullName).ToList();


Sorry, just noticed this is a VB.Net question. Not my strong suit, something like this:

Dim lstFiles As List(Of FileInfo) = New List(Of FileInfo)()

For Each file In Directory.GetFiles("C:\yourfolder\", "*.pdf")
    Dim fInfo As FileInfo = New FileInfo(file)
    lstFiles.Add(fInfo)
Next

Dim lstPDFs = lstFiles.OrderBy(Function(f) f.CreationTime).[Select](Function(f) f.FullName).ToList()
 
Share this answer
 
v2
Sorry I didn't have the time earlier when I posted my comment... here is the quick proof of concept console app utilizing LINQ.
VB
Dim FileList = New DirectoryInfo("C:\FolderName\").GetFiles("*.pdf")
Dim DisplayList = From EachFile In FileList Order By EachFile.CreationTime

For Each DisplayFile In DisplayList
  Console.WriteLine(DisplayFile.Name)
Next

Could probably be simplified further but like 'LittleGreenDude' VB is not my forte
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900