private string CreateUniqueFileName(FileInfo file, string destination) { string savePath = Path.Combine(destination, file.Name); int fileCount = 0; string fileExt = ""; string fileNameWithoutExt = ""; if (File.Exists(savePath)) { DirectoryInfo dir = new DirectoryInfo(destination); FileInfo[] saveFiles = dir.GetFiles(file.Name + "*"); fileNameWithoutExt = FilePathDeconstructor.PathWithoutExt(file.Name); fileExt = FilePathDeconstructor.GetExt(file.Name); fileCount = FolderOperations.CheckFilesWithName(file.Name, destination); savePath = Path.Combine(destination, String.Format("{0}({1}).{2}", fileNameWithoutExt, fileCount, fileExt)); } while (File.Exists(savePath)) { fileCount++; savePath = Path.Combine(destination, String.Format("{0}({1}).{2}", fileNameWithoutExt, fileCount, fileExt)); } return(savePath); }
public static int CheckFilesWithName(string name, string path) { return(Directory.GetFiles(path, "*" + FilePathDeconstructor.PathWithoutExt(name) + "*").Count()); }