列表框的文件需要按文件创建日期/时间排序 [英] Files to listbox need ordering by file creation date/time
本文介绍了列表框的文件需要按文件创建日期/时间排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下填充列表框但我需要先按创建日期/时间排序文件。最新的列表顶部。听起来很容易,但我无法弄清楚如何。任何帮助非常感谢!提前致谢
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
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
推荐答案
这个怎么样:
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();
很抱歉,注意到这是一个VB.Net问题。不是我的强项,比如这样:
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()
对不起,我发布评论时没有时间......这是使用LINQ的概念控制台应用快速验证。
Sorry I didn't have the time earlier when I posted my comment... here is the quick proof of concept console app utilizing LINQ.
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
可能会进一步简化,但像'LittleGreenDude'VB不是我的强项
Could probably be simplified further but like 'LittleGreenDude' VB is not my forte
这篇关于列表框的文件需要按文件创建日期/时间排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文