469,626 Members | 883 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,626 developers. It's quick & easy.

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

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
Nov 15 '05 #1
2 1551
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]
- mv*@spam.guard.caspershouse.com

"Rosa" <Ro**********@hotmail.com> wrote in message
news:39**************************@posting.google.c om...
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

Nov 15 '05 #2
Thank Nicholas! I will carry on working down the lines of this solution.

Rosa

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:<e$**************@TK2MSFTNGP10.phx.gbl>...
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]
- mv*@spam.guard.caspershouse.com

"Rosa" <Ro**********@hotmail.com> wrote in message
news:39**************************@posting.google.c om...
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

Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Tony Liu | last post: by
8 posts views Thread by Braky Wacky | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.