C#: what is the simplest way to sort the directory names and pick the most recent one?

satya picture satya · Jan 19, 2010 · Viewed 13k times · Source

I have a list of directories in a parent directory. These directories will be created in a format like 00001, 00002, 00003... so that the one with the bigger trailing number is the recent one. in the above instance, it is 00003. I want to get this programmatically.

thanks for any help..

Answer

RichardOD picture RichardOD · Jan 19, 2010

Try this:

var first = Directory.GetDirectories(@"C:\")
   .OrderByDescending(x => x).FirstOrDefault();

Obviously replace C:\ with the path of the parent directory.