0

I have a pretty big codebase in .NET 4.7.2 which does a lot of file system manipulation and pretty often calls Directory.Delete(path, true) method, which sometimes yields to IOException or UnauthorizedAccessException.

According to a thread it's because someone else owns a windows handle to a directory.

The code base is using multiple managed threads and I'd like to find out if some other thread/resource holds a handle to a directory.

Is there a way to accomplish that without actually calling Directory.Delete and receiving an exception?

Andriy Shevchenko
  • 1,117
  • 6
  • 21
  • There is no method in .NET Framework for this. And even if there was, in the time between checking and executing the Delete another process may create or close a handle. It can be done by some low-level techniques as demonstrated by SysInternal's process explorer. – Klaus Gütter Nov 26 '21 at 05:59
  • 1
    See also here: https://stackoverflow.com/questions/177146/how-do-i-get-the-list-of-open-file-handles-by-process-in-c/5372541 – Klaus Gütter Nov 26 '21 at 06:07

0 Answers0