Query filesystem for list of files with certain attributes?
I'd like to be able to query a folder (and subfolders) and get a list of files meeting certain criteria on specific attributes...so, for example, all files that have:
under c:\somefolder
file_extension = ".abc" filesize between x and y KB (filename like '%this' or filename like '%that%' and filename n开发者_如何学Cot like '%somethingelse%' modifieddate between date1 and date2Is this sort of thing possible using LINQ, and what would the syntax look like?
Yes. The syntax would look something like:
var files = from file in new DirectoryInfo(@"c:\some_folder")
.GetFiles("*.abc", SearchOption.AllDirectories)
let lengthInKb = file.Length / 1024D
let name = file.Name
let modifiedDate = file.LastWriteTime.Date
where (lengthInKb >= x && lengthInKb <= y)
&& (name.EndsWith("this") || name.Contains("that"))
&& !name.Contains("somethingelse")
&& (modifiedDate >= date1 && modifiedDate <= date2)
select file;
精彩评论