From: "Bobby C. Jones" bjones beazer on
"AA2e72E" <AA2e72E(a)discussions.microsoft.com> wrote in message
news:71A689C6-1107-4D93-A3B1-1782D4898F9E(a)microsoft.com...
> Thanks. However, I don't think I asked the right question. I'll try
> again:
>
> Given:
>
> System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"c:\test");
> IEnumerable<System.IO.FileInfo> fileList =
> dir.GetFiles("*.txt",System.IO.SearchOption.AllDirectories);
>
> fileList will contain files in the directory tree c:\test and files by the
> same name will exist in c:\test\one\myfile.txt and c:\test\two\myfile.txt
> etc.
>
> I would like to be able to pick the myfile.txt that has the latest
> creation
> time; obviously when a file exixts uniquely i.e. in one sub directory in
> the
> tree, it will have the latest creation time (by default) and will get
> picked.
>
> I hope I have explained this adequately: thanks for your help.
>


Perhaps something like this

DirectoryInfo dir = new DirectoryInfo(@"C:\test");

var newestFiles = from file in dir.GetFiles("*.txt",
SearchOption.AllDirectories)
orderby file.LastWriteTime descending
group file by file.Name.ToUpper() into files
select files.ElementAt(0);

--
Bobby C. Jones
http://bobbycjones.spaces.live.com


From: AA2e72E on

A 'quick' test seems to indicate that this will work; I'll do some more
testing. Thanks.
From: Peter Duniho on
AA2e72E wrote:
> Thanks. I am after
>
> 'That is, is the operation something like "look for any file named X, return
> the one file named X that is the most recent"?'
>
> I am inclined to agree that there may not be a LINQ only solution although I
> thought Linq87 (Max - Grouped) in the 101 Linq samples may provide a suitable
> basis for a solution.

As I wrote before, if that's what you are trying to do, the solution is
simple. Just pass the filename as the search pattern for GetFiles(),
and then use the LINQ example I posted first.

Pete