Question

How to get the path of the recent or latest file based on creation time (say 'test.xml) located in many sub directories within a main directory.

Was it helpful?

Solution

You can use LINQ:

Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories)
         .OrderBy(File.GetLastWriteTime)
         .Last()

If you're not using .Net 4.0, change that to

Directory.GetFiles(path, "*", SearchOption.AllDirectories)
         .OrderBy(p => File.GetLastWriteTime(p))
         .Last()

This is somewhat slower, but will work in .Net 3.5.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top