c#LINQ:根据文件大小过滤文件列表 [英] c# LINQ: Filter List of Files based on File Size
本文介绍了c#LINQ:根据文件大小过滤文件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一些使用LINQ来根据文件大小提交文件列表的帮助.我有一些代码,但它使用的是file.length而不是FileInfo(file).length.我不知道如何在表达式中实现对象'FileInfo'.帮助吗?
I need some help using LINQ to filer a list of files based on the file size. I have some code but it's using file.length instead of FileInfo(file).length. I don't know how to implement an object 'FileInfo' in the expression. HELP?
{
IEnumerable<string> result = "*.ini,*.log,*.txt"
.SelectMany(x => Directory.GetFiles("c:\logs", x, SearchOption.TopDirectoryOnly))
;
result = result
.Where(x => (x.Length) > "500000")
;
}
推荐答案
您应该可以执行以下操作.
You should be able to do something like this.
使用DirectoryInfo,GetFiles将返回FileInfo的集合,而不是字符串.
Using a DirectoryInfo, the GetFiles will return a collection of FileInfo instead of strings.
new DirectoryInfo(@"c:\logs")
.GetFiles("*.ini,*.log,*.txt", SearchOption.TopDirectoryOnly)
.Where(f => f.Length > 500000);
当然,如果愿意,您总是会内联创建FileInfo.
Of course you would always create FileInfo inline if you wanted to.
如果您只想返回文件名.
If you just wanted the filenames back..
IEnumerable<string> results = new DirectoryInfo(@"c:\logs")
.GetFiles("*.ini,*.log,*.txt", SearchOption.TopDirectoryOnly)
.Where(f => f.Length > 500000)
.Select(f => f.FullName);
这篇关于c#LINQ:根据文件大小过滤文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文