I have a File.Delete in my finally clause like so:
finally
{
//remove the temporary file
if(File.Exists(transformedFile))
File.Delete(transformedFile);
}
According to the C# documentation, calling File.Delete on a nonexistent file will not throw any exceptions.
Is it okay to remove the File.Exists wrapped, or will that expose me to possible additional exceptions?
If you need it, it’s insufficient, as the file could be deleted after you confirm that it exists. In a case like this, the best practice is to simply try to delete the file. If it fails with a “file not found” type of error, then you’ll know the file didn’t exist. This removes an extra operation and avoids any kind of race window.