I have this code where I am copying file from one location to another -
try
{
var destination = Path.Combine(targetPath, fileInfo.Name);
fileInfo.CopyTo(destination); //error thrown at this line
}
catch (Exception ex)
{
//log error
}
because the destination path was already having file with same name, it threw the exception.
But to my surprise, the exception object was null.
I have fixed the issue by using CopyTo overload method to overwrite the existing file -
fileInfo.CopyTo(destination, true); //overwrite if file exists
which has fixed the issue.
But, my mind still is wondering - why exception object was null?
Any insights please.