ListBox按文件创建时间排序



我的listBox1中充满了txt文件的名称。它会自动按字母顺序对它们进行排序。但我需要按创建时间对这些文件进行排序,从最新到最旧。有人能帮我吗?

你没有给我们太多的东西:p

但我会尽我最大的努力来帮助你。因为我不知道你是如何填写列表的(手工或代码(,我真的无法指导你。

但假设你是通过代码完成的。我键入的以下片段可能会帮助您找到需要的

//array of file names (these need to be paths to files, so if you do not prefix it with ./path/ or anything, then the executable needs to be in the same folder)
//If you are debugging, make sure the paths are correct if you expect them to be in the same folder, it should then be in the debug folder.
//If you included some test files in the solution and want them copied to your debug folder. Click on them, go to the properties pane, and set build action to content, and one of the other properties should read copy of newer instead of never copy
var fileNames = new string[]{"1.txt", "2.txt", "3.txt"};
var fileInfos = fileNames.Select(f => new FileInfo(f));
var orderedFileInfos = fileInfos.OrderBy(f => f.CreationTime);

orderedFileInfos现在是按创建时间升序排列的FileInfo类型列表。

这是您可以用来填充ListBox 的内容

我希望我帮助你的项目取得进展。