Looking for an elegant solution to find a file within a fileshare

  • Thread starter Thread starter Rosa
  • Start date Start date
R

Rosa

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
 
Rosa,

This is probably the best solution that you have. You could add all the
files into a sortable data source, like a data set (with one column being
the file name, and another being the date), and then sorting based on the
criteria you have. However, that would probably take longer, because you
have to cycle through the files in the directory and then place them in the
structure, and then sort the structure.

You are reducing most of the overhead here, and this is probably the
quickest way. The only other thing that I can think of would be to use
Index Serivces and then interop with that (I am not positive, but I seem to
recall that index services is geared for this kind of thing).

Hope this helps.
 
Thank Nicholas! I will carry on working down the lines of this solution.

Rosa

Nicholas Paldino said:
Rosa,

This is probably the best solution that you have. You could add all the
files into a sortable data source, like a data set (with one column being
the file name, and another being the date), and then sorting based on the
criteria you have. However, that would probably take longer, because you
have to cycle through the files in the directory and then place them in the
structure, and then sort the structure.

You are reducing most of the overhead here, and this is probably the
quickest way. The only other thing that I can think of would be to use
Index Serivces and then interop with that (I am not positive, but I seem to
recall that index services is geared for this kind of thing).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Rosa said:
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
 
Back
Top