0

I am trying to get the text files from a certain directory and list them by date modified into a listBox. Here is my current code, but I have no idea how do order them by date modified.

DirectoryInfo di = new DirectoryInfo(@".\notes\");
foreach (FileInfo fi in di.GetFiles("*.txt"))
    listBox1.Items.Add(fi.Name.Substring(0, fi.Name.Length - 4))
4

1 回答 1

0

Use the built-in OrderBy method:

foreach(FileInfo fi in di.GetFiles("*.txt").OrderBy (d => d.LastWriteTime))
{
    listBox1.Items.Add(fi.Name.Substring(0, fi.Name.Length - 4));
}

If you want them in descending order, there is OrderByDescending.

于 2013-11-02T02:30:40.860 回答