if (Directory.Exists(dir)) Directory.Delete(dir, true);
The above code checks if the directory exists, if so, removes it. There is a possibility that between verification exists and is deleted, that the directory has been added or deleted.
Beyond the call. Remove and discard exceptions, is there a proper way to prevent this race condition?
edit:
The reason to avoid dealing with race conditions with exception handling is because exceptions should not be used for control flow.
Is a file system lock ideal?
, , dir , , , Directory.Delete , , , . , , , , :
dir
Directory.Delete
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. }
, , Directory.Exists
Directory.Exists
if (Directory.Exists(dir)) { try { Directory.Delete(dir, true); } catch (DirectoryNotFoundException e) { // do nothing, it means it doesn't exist } }
/? . if, .
if
, ?, , a object singleton lock constructure?
object
lock
, handle, .
handle
, , . , : , - .
? .NET . , :
IOException, , . > --, , , - false, - . --. --. --.UnauthorizedAccessException.ArgumentExceptionpath - , . GetInvalidPathChars.ArgumentNullException null.PathTooLongException, . , Windows 248 260 .DirectoryNotFoundException. --(, ).
IOException, , . > --, , , - false, - . --. --. --.
IOException
UnauthorizedAccessException.
UnauthorizedAccessException
ArgumentExceptionpath - , . GetInvalidPathChars.
ArgumentException
GetInvalidPathChars
ArgumentNullException null.
ArgumentNullException
PathTooLongException, . , Windows 248 260 .
PathTooLongException
248
260
DirectoryNotFoundException. --(, ).
DirectoryNotFoundException