tags:

views:

70

answers:

2
+1  Q: 

Long files names

Hi,

How to get the longest files names in a directory using C#?

+13  A: 
Directory.GetFiles(myPath)
    .OrderByDescending(s => s.Length)
    .Take(howManyYouWant);
Yuriy Faktorovich
+1 beat me to it... (might want to suggest adding Take)
Nix
`Directory.GetFiles(myPath)` if you want all files, not just directories
Lou Franco
@Nix already done.
Yuriy Faktorovich
And if you're using .NET 4 then `EnumerateFiles` might be more efficient than `GetFiles`.
LukeH
A: 
var filelist = Directory.GetFiles(<directorypathandname>);
var result = filelist.Where( f => f.Length == filelist.Max( f2 => f2.Length));

Now result will contain all files with the longest name. Usually only one I guess, but all of them if they have identical length, and is longest.

Øyvind Bråthen