public static void AssureUniqueFilename(string folder, string fileName, out string newFile) {
int cnt = 1;
newFile = Path.Combine(folder, fileName);
while (File.Exists(newFile)) {
string fName = Path.GetFileNameWithoutExtension(fileName);
string ext = Path.GetExtension(fileName);
string newFileName = fName + " (" + cnt++ + ")";
if (!string.IsNullOrEmpty(ext)) {
newFileName = newFileName + ext;
}
newFile = Path.Combine(folder, newFileName);
}
}