I'm facing a challenge with an app doing some process then it moves a file to a new directory. It is working fine, but if a file with the same name is found, I get an unhandled exception.
I do not want to overwrite the file if it already exists, and I don't want to give users a message. If the file already exists i want to move the file to NewDirectory and rename the file with a different name.
The solutions I try never catch the unhandled exception.
string NewDirectory = (@"D:\Files\edited\");
Directory.CreateDirectory(NewDirectory);
string seconds = DateTime.Now.ToString("-ss");
string NewName = "Example Txt" + "-2.2-";
string FullPath = (NewDirectory + NewName + ".txt");
if (File.Exists(FullPath))
{
File.Move(file, NewDirectory + NewName + seconds + ".txt");
Console.WriteLine(NewName + seconds);
}
else
File.Move(file, NewDirectory + NewName + ".txt");
Console.WriteLine(NewName);