Donate

Show All Folders Inside Directory Sorted By Creation Time Using Linq In C# And VB.NET

Here's a solution using LINQ that will retrieve all folders inside a specific directory in order of creation time.
C#
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
string path = @"C:\";
if (Directory.Exists(path)) {
    DirectoryInfo dir = new DirectoryInfo(path);
    DirectoryInfo[] directories = dir.GetDirectories().
                                    OrderByDescending(p => p.CreationTime).
                                    ToArray();
    if (directories.Length > 0) {
        foreach (DirectoryInfo directory in directories) {
            ListBox1.Items.Add(directory.Name);
        }
    }
}

VB.NET
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Dim path As String = "C:\"
If Directory.Exists(path) Then
    Dim dir As New DirectoryInfo(path)
    Dim directories As DirectoryInfo() = dir.GetDirectories().
                                             OrderByDescending(Function(p) p.CreationTime).
                                             ToArray()
    If directories.Length > 0 Then
        For Each directory As DirectoryInfo In directories
            ListBox1.Items.Add(directory.Name)
        Next
    End If
End If

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.

Invalid nested tag div found, expected closing tag input