I would like to create a method which takes either a filename as a string
or a FileInfo
and adds an incremented number to the filename if the file exists. But can't quite wrap my head around how to do this in a good way.
For example, if I have this FileInfo
var file = new FileInfo(@"C:\file.ext");
I would like the method to give me a new FileInfo with C:\file 1.ext if C:\file.ext existed, and C:\file 2.ext if C:\file 1.ext existed and so on. Something like this:
public FileInfo MakeUnique(FileInfo fileInfo)
{
if(fileInfo == null)
throw new ArgumentNullException("fileInfo");
if(!fileInfo.Exists)
return fileInfo;
// Somehow construct new filename from the one we have, test it,
// then do it again if necessary.
}