I have a little problem sorting files.
My program should allow me to sort the files of a directory by size and by date. The date works fine but when I try to sort by size, it returns an error.
This is my relevant code:
if (orden.Equals("tam"))
{
ficheroo = dirInfoo.GetFiles(filtro, SearchOption.AllDirectories).OrderBy(f => new FileInfo(f).Length).ToList();
}
the error is in the use of new FileInfo(f).Length
and the error is:
La mejor coincidencia de método sobrecargado para 'System.IO.FileInfo.FileInfo(string)' tiene algunos argumentos no válidos
This translates to:
The best overloaded method match for 'System.IO.FileInfo.FileInfo (string)' has some invalid arguments
DirectoryInfo.GetFiles
already returns a FileInfo[]
- so you don't need to convert each entry into a FileInfo
using the constructor, as you're trying to do now. You can just use:
ficheroo = dirInfoo.GetFiles(filtro, SearchOption.AllDirectories)
.OrderBy(f => f.Length)
.ToList();
(As a side note, it's worth seeing how using vertical space makes your code easier to read than having everything on one enormous line.)
See more on this question at Stackoverflow