如何检索目录中的文件列表,按名称排序 [英] How to retrieve list of files in directory, sorted by name
问题描述
Directory.GetFiles(folder)
但是我需要按字母顺序排列的结果,因为它们都是数字,而我需要知道目录中最高的数字。当然,我可以把它们放到一个数组/列表对象中,然后做一个排序,但是我想知道是否有一些过滤器/参数?
它们都被命名带前导零。喜欢:
00000000001.log
00000000002.log
00000000003.log
00000000004.log
..
00000463245.log
00000853221.log
00024323767.log
最简单的方法是什么?我不需要得到其他文件,只是最大/最新的数字。
var files = Directory.EnumerateFiles(folder)
.OrderByDescending(filename => filename);
( EnumerateFiles
方法是新的。 NET 4,如果您使用的是早期版本,您仍然可以使用 GetFiles
编辑:实际上你不需要对文件名进行排序,如果你使用 MaxBy 方法code.google.com/p/morelinq/\"> MoreLinq :
var lastFile = Directory.EnumerateFiles(folder ).MaxBy(filename => filename);
I am trying to get a list of all files in a folder from C#. Easy enough:
Directory.GetFiles(folder)
But I need the result sorted alphabetically-reversed, as they are all numbers and I need to know the highest number in the directory. Of course I could grab them into an array/list object and then do a sort, but I was wondering if there is some filter/parameter instead?
They are all named with leading zeros. Like:
00000000001.log
00000000002.log
00000000003.log
00000000004.log
..
00000463245.log
00000853221.log
00024323767.log
Whats the easiest way? I dont need to get the other files, just the "biggest/latest" number.
var files = Directory.EnumerateFiles(folder)
.OrderByDescending(filename => filename);
(The EnumerateFiles
method is new in .NET 4, you can still use GetFiles
if you're using an earlier version)
EDIT: actually you don't need to sort the file names, if you use the MaxBy
method defined in MoreLinq:
var lastFile = Directory.EnumerateFiles(folder).MaxBy(filename => filename);
这篇关于如何检索目录中的文件列表,按名称排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!