Looking for latest filename
-
I have an application that takes images and stores them automatically in a folder on my local drive. The application auto increments the file name by adding 1 to the last one taken. I name the images as photo_00001 and the next taken photo names as photo_00002. Now I want to look through the folder and select the last image taken that is the one with the highes name. Any ideas how I can achieve that?
a novice
-
I have an application that takes images and stores them automatically in a folder on my local drive. The application auto increments the file name by adding 1 to the last one taken. I name the images as photo_00001 and the next taken photo names as photo_00002. Now I want to look through the folder and select the last image taken that is the one with the highes name. Any ideas how I can achieve that?
a novice
Hi, there are a couple of solutions: - use Directory.GetFiles() to get all the file names, then find the "highest" file name in those. - use Directory.GetFiles() to get all the file names, then find the latest file by comparing creation times using File.GetCreationTime() - do a binary search for filenames that match your syntax, checking existence with File.Exists() - teach your app to save the latest file name (a single string) in a fixed location, probably a file in the same folder ("latestFile.txt"). - teach your app to save the latest file name (a single string) in the registry somewhere. Probably not good: - do a linear search for filenames that match your syntax, checking existence with File.Exists(); is slow and might return a hole in the sequence if you somehow deleted an older file. :)
Luc Pattyn [Forum Guidelines] [My Articles]
This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.