4
if (Directory.Exists(dir))
    Directory.Delete(dir, true);

The above code checks if the directory exists, if so, deletes it. There's the chance that between the exists check and the delete, that the directory was added or removed.

Aside from calling .Delete and throwing away the exceptions, is there a proper way to prevent this race condition?

edit:

The reason to avoid battling the race condition with exception handling is because exceptions should not be used for control flow.

An ideal solution would be a file system lock of some sorts?

Community
  • 1
  • 1
Joseph Lennox
  • 2,947
  • 1
  • 23
  • 24

3 Answers3

4

If the desired end result is to ensure that the directory dir does not exist, regardless of whether it existed or not, then you should call Directory.Delete and catch any exception it may throw, without bothering to check if the directory exists or not. Then you should check if the directory exists to see if you are good to go, or if your operation has failed for some other reason:

try {
    Directory.Delete(dir, true);
} catch {
    // Ignore any exceptions
}
if (Directory.Exists(dir)) {
    // The above has failed to delete the directory.
    // This is the situation to which your program probably wants to react.
}
Sergey Kalinichenko
  • 675,664
  • 71
  • 998
  • 1,399
3

The best you can do is a mix between Directory.Exists and exception handling

if (Directory.Exists(dir))
{
    try 
    {
        Directory.Delete(dir, true);
    }
    catch (DirectoryNotFoundException e)
    {
        // do nothing, it means it doesn't exist
    }
}

Why not just having a try / catch? Because exceptions are not supposed to be used to handle application flow. You try to avoid the exception with the if statement but in the corner case it happens, you handle it.

Claudio Redi
  • 63,880
  • 13
  • 118
  • 146
1

Is your code the only thing that can delete or add such folder?
If so, why don't you just add the critical section to your code, like a object singleton and lock constructure?

If you want to avoid deleting an already deleted folder, you can add a process handle for it so other processes can't destroy it.

If you want to avoid a situation where this code wouldn't delete needed directory, I can't imagine a good solution for this. Imagine the situation as: your code runs, and after it finishes, somebody re-creates the folder.

Why not just catch the specific exception in this case? It's a natural way in .NET to handle unexpected situations. There are a lot of situations you can't predict:

Exceptions

IOException
A file with the same name and location specified by path exists. -or-
The directory specified by path is read-only, or recursive is false and path is not an empty directory. -or-
The directory is the application's current working directory. -or-
The directory contains a read-only file. -or-
The directory is being used by another process.

UnauthorizedAccessException
The caller does not have the required permission.

ArgumentException
path is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters by using the GetInvalidPathChars method.

ArgumentNullException path is null.

PathTooLongException
The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters and file names must be less than 260 characters.

DirectoryNotFoundException
path does not exist or could not be found. -or-
The specified path is invalid (for example, it is on an unmapped drive).

CarenRose
  • 1,142
  • 11
  • 18
VMAtm
  • 26,645
  • 17
  • 75
  • 107