How do I check whether File.Delete() will succeed without trying it, in C#?

The problem with implementing FileIsDeletableByCurrentUser is that it’s not possible to do so. The reason is the file system is a constantly changing item.

In between any check you make to the file system and the next operation any number of events can and will happen. Including …

  • Permissions on the file could change
  • The file could be deleted
  • The file could be locked by another user / process
  • The USB key the file is on could be removed

The best function you could write would most aptly be named FileWasDeletableByCurrentUser.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)