Hi,
I'm looking for an elegant solution on how to find the youngest file
within a given directory.
At the moment I'm storing all files in an array and loop through it
comparing the creation date as follows:
private string FileShare(string strURL)
{
string [] files;
DateTime datLastWriteTime;
DateTime datNewLastWriteTime;
string strNewestDoc ;
files = Directory.GetFiles(strURL);
if (files.Length > 0)
{
datNewLastWriteTime = File.GetLastWriteTime(files[0]);
strNewestDoc = files[0];
//find latest doc
foreach(string fileName in files)
{
datLastWriteTime = File.GetLastWriteTime(fileName);
if (datLastWriteTime > datNewLastWriteTime)
{
datNewLastWriteTime = datLastWriteTime;
strNewestDoc = fileName;
}
}
return strNewestDoc;
}
else
return "";
}
This is ok for a directory with a few documents, but when there is
hundreds of them, I don't think performance will be very good...
Please help
Thanks
Rosa