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.
+4
A:
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.
SLaks
2010-10-20 12:58:01
That's no correct solution. It gives you the max time, but not the File Path as requested.
TToni
2010-10-20 13:15:23
@TToni: You're right; fixed. Thank you. It would be faster with a loop or `Aggregate` instead of a sort, but I'm too lazy for that.
SLaks
2010-10-20 13:18:39
Thanks a Lot. Gr8!
sukumar
2010-10-21 15:08:31