Using LINQ I would like to retrieve all files under a given directory that are less/greater than a specified file size.
I have the following code that returns a List at present:
public static List<string> getFs(string sDir)
{
var files = Directory.EnumerateFiles(sDir, "*.*", SearchOption.AllDirectories)
.Where(s => s.ToLower().EndsWith(".psd"));
return files.ToList();
}
I found the following code courtesy of Monsieur Skeet that seems to access file size:
long diskSpace = (from directory in Directory.EnumerateDirectories(@"c:\")
from file in Directory.EnumerateFiles(directory)
select file)
.Sum(file => new FileInfo(file).Length);
How would I adapt the file size aspect of this into my existing code or is this the wrong approach in the context of what I have already?
You just need to add an additional
Where… As an example:Of course, you could also combine the two
Whereclauses; I kept them separate for clarity. This is equally valid: