如何检索目录中的文件列表,按名称排序 [英] How to retrieve list of files in directory, sorted by name
问题描述
我想从C#中的文件夹中的所有文件的清单。很容易:
Directory.GetFiles(文件夹)
不过,我需要的结果按字母顺序排序颠倒,因为他们都是数字和我需要知道在目录。当然,我可以抓住他们到一个数组/列表对象,然后做一个排序,但我想知道是否有某种过滤器/参数呢?
它们都被命名为带前导零。这样的:
00000000001.log
00000000002.log
00000000003.log
00000000004.log
..
00000463245.log
00000853221.log
00024323767.log
请告诉我最简单的方法?我不需要得到其他文件,只是最大/最新的号码。
从文件var中的文件=在Directory.GetFiles(文件夹)
排序依据文件降
选择文件;
VAR最大= files.First();
如果您是最高人数和后真日志文件被命名为喜欢你的建议,怎么样:
Directory.GetFiles(文件夹)。长度
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 = from file in Directory.GetFiles(folder)
orderby file descending
select file;
var biggest = files.First();
if you are really after the highest number and those logfiles are named like you suggested, how about:
Directory.GetFiles(folder).Length
这篇关于如何检索目录中的文件列表,按名称排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!