C# 列表框按文件创建时间排序

C# 列表框按文件创建时间排序,c#,C#,我的listBox1中填充了txt文件的名称。它是按字母顺序自动排序的。但我需要按创建时间对这些文件进行排序,从最新到最旧。有人能帮帮我吗?你没有给我们太多的支持:p 但我会尽我最大的努力来帮助你。因为我不知道您是如何填写列表的(手工填写或代码填写),所以我无法真正指导您 但假设你是通过代码来完成的。我键入的以下代码片段可能会帮助您找到所需内容 //array of file names (these need to be paths to files, so if you do not pre

我的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类型列表

这是您可以用来填充列表框的内容


我希望我能帮助您在项目中取得进展。

您可以在提问时分享您的代码吗?你试过什么?我基本上什么都没试过。我试着用谷歌搜索,但什么也没找到。或者可能会有帮助(System.IO名称空间)我尝试用谷歌搜索它,但没有找到任何东西,只是用第一个谷歌链接找到了这个,还有许多其他的。请分享你到目前为止的代码,即使它与按创建日期排序无关。我们不可能在看不到任何代码的情况下就如何对这些文件进行排序向您提供建议。我们需要了解如何获取文件列表,如何将它们放入
列表框
,等等。