c# – Long files names – Education Career Blog

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

,

Directory.GetFiles(myPath)
    .OrderByDescending(s => s.Length)
    .Take(howManyYouWant);

,

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.

Leave a Comment