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(strin g strURL)
{
string [] files;
DateTime datLastWriteTim e;
DateTime datNewLastWrite Time;
string strNewestDoc ;
files = Directory.GetFi les(strURL);
if (files.Length > 0)
{
datNewLastWrite Time = File.GetLastWri teTime(files[0]);
strNewestDoc = files[0];
//find latest doc
foreach(string fileName in files)
{
datLastWriteTim e = File.GetLastWri teTime(fileName );
if (datLastWriteTi me > datNewLastWrite Time)
{
datNewLastWrite Time = datLastWriteTim e;
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
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(strin g strURL)
{
string [] files;
DateTime datLastWriteTim e;
DateTime datNewLastWrite Time;
string strNewestDoc ;
files = Directory.GetFi les(strURL);
if (files.Length > 0)
{
datNewLastWrite Time = File.GetLastWri teTime(files[0]);
strNewestDoc = files[0];
//find latest doc
foreach(string fileName in files)
{
datLastWriteTim e = File.GetLastWri teTime(fileName );
if (datLastWriteTi me > datNewLastWrite Time)
{
datNewLastWrite Time = datLastWriteTim e;
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
Comment