I have a client who wants to be able to save files to a specific directory with an auto increment system in place (NNN.fileextension), where the first file that is uploaded starts with the name of “001”, the second “002” and so forth. That is all fine, however, I am stumped on how to be able to loop through the existing files in the directory and find the last file that was uploaded based on the filename pattern specified earlier.
How can I loop through the files, determine if the file has the pattern of “NNN” (N representing numeric) and acquire the last file to be uploaded?
You could use Directory.GetFiles(string, string) which accepts a search pattern for files as a second argument (the question mark
?acts as a single character place holder).So to find the max id you should iterate through the files found, convert the file name to an integer value and select the maximum value.