Most recent file

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have an application where I need to find the most recent file placed in a
folder. For example, my users periodically place an image file with the
format "ImageXXXX.jpg" (where XXXX is some arbitrary number) and I need to
get the latest file in the folder. Does anyone have a .NET solution to this
problem. Any help would be greatly appreciated
Thanks,
Lee
 
Would you not be able to use FileSystemWatcher?

It would get hit the instant a file would be created in your folder - or
subfolder.

Miro
-New to VB.net so you may want some other opinions.
 
Not really. I don't think I explained the problem very well. I have a list
of files with a *.jpg extension and I need to find the most recent file in
the list which may not be the most recent file in the folder because other
people may have put files in there without the *.jpg extension.
 
You could still tie into filesystemwatcher. Just iterate the files with
that extension you seek when watcher indicates a file has changed, and look
at their properties of all files using the FileInfo class.

something like:FileInfo fFile = new FileInfo(theiteratedfilename);
DateTime fred = fFile.CreationTime.ToString()

Subtract the created date from datetime.now using timespan classes subtract
method to give you span.Seconds, and you have how long ago the file was
created.

If that figure is less than the last files figure then the current file in
the iteration is the most recent. Just repeat until all the files are
checked.

Regards

John Timney (MVP)
 
Back
Top