According to the
File.Move documentation[
^] an IOException is thrown when the distination file already exists.
You can use
File.Copy(source, destination, true)[
^] to copy the file to the distination and overwrite any existing file. and then use
File.Delete[
^] to delete the source/original file
Edit: your if-else-if can be simplified
if (File.Exists(tempName) && !File.Exists(imageName))
{
File.Move(tempName, imageName);
}
else if (File.Exists(tempName) && File.Exists(imageName))
{
File.Move(tempName, imageName);
}
==
if (File.Exists(tempName)
File.Move(tempName, imageName);