Codeman
Codeman

Reputation: 12375

Should I call File.Exists before calling File.Delete?

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?

Upvotes: 13

Views: 12083

Answers (6)

Ismail Hawayel
Ismail Hawayel

Reputation: 2453

File.Delete does not throw a FileNotFoundException, it does not care. It will throw an exception only if the path is invalid, as in it has directories that do not exist, in that case it will throw DirectoryNotFoundException.

This code won't throw any exception.

var file = new FileInfo(@"C:\doesnotexist.file11111");
file.Delete();

This will throw DirectoryNotFoundException

var file = new FileInfo(@"C:\bad.dir\doesnotexist.file11111");
file.Delete();

MSDN File.Delete

Upvotes: 5

Guffa
Guffa

Reputation: 700850

There is one situation where checking Exists before Delete prevents an exception. If you have a file name with an invalid path, the Exists method returns false.

Then it depends on what behaviour you want. In some sitations an invalid path should cause an exception.

Also, just because the file exists doesn't mean that it's always possible to delete it (at that time). You still need the exception handling for unforseen problems.

Upvotes: 10

babakgh
babakgh

Reputation: 269

you still can get other type of exceptions. for example IOException if file is in used.

so if you insist to delete file in finally block and want to be sure of not getting exception just put another try-catch around File.delete.

finally
{
   try 
   {
     //remove the temporary file
     File.Delete(transformedFile);
   }
   catch
   {
   }
}

but if your goal is to delete file in any case, I think it would be good idea if you open file exclusively and in this case you should close file first and then delete it.

Upvotes: 1

myermian
myermian

Reputation: 32525

I've asked a similar question to this question before, and I came to this conclusion:

It depends. Yes, I know... not really a straightforward answer, but here are a few notes to take to determine if your situation requires a file exists check and/or a try { ... } catch { ... } block around the File.Delete(...) method.

  • Did your code generate the file? If so, you don't really need to do the check to see if the file exists.
  • Do you care about handling other issues? Remember, you are working with a file system, and the MSDN documentation clearly shows you that the method could generate exceptions due to other circumstances.

Upvotes: 0

slugster
slugster

Reputation: 49965

Let's look at this logically and critically. Assuming the MSDN doco isn't 100% accurate (it does contain occassional errors) and a FileNotFoundException could be thrown, it would caused by either :

  1. the file never existed to begin with (you exited the try and entered the finally before the file was created)

  2. you have a programmatic error (you've assembled the file path or name incorrectly)

One would assume that option #2 shouldn't happen, because you test for that, if it can still happen then you have smelly code. That leaves option #1 as the only viable option, in which case you don't care about the exception, so you would just catch it and move on. This means your specific question is largely redundant even if a FileNotFoundException could be thrown.

However...
I would still wrap the File.Delete with a try/catch because IMVHO there are still two exceptions that are possible and you need to take notice of: IOException and UnauthorizedAccessException. If either of those occur you should look at some sort of mitigation (maybe set up the file for deletion upon next reboot and/or notify the user in some way).

Upvotes: 1

David Schwartz
David Schwartz

Reputation: 182883

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.

Upvotes: 14

Related Questions