1

I'm curious if the method, Directory.GetLastWriteTime puts a lock on the directory in question momentarily?

We're seeing the error, 'The directory is not empty' when trying to do a Directory.Delete(filePath, true) on some directory after calculating if it's old or not. This does not happen every time.

After further research, I understand that Directory.Delete(filePath, true) will throw this error when a sub-directory of the directory to be deleted is open in File Explorer (Cannot delete directory with Directory.Delete(path, true)). I am able to reproduce this behavior by opening one of the sub-directories in Windows Explorer and then trying to do a Directory.Delete(filePath, true).

Which leads me to my question - we are not opening these directories through code anywhere, and the only other thing we do with the directory is GetLastWriteTime. Could this method be causing the same behavior?

Talen Kylon
  • 1,908
  • 7
  • 32
  • 60
  • There are a lot of reasons for this error, the last write time is the least likely. The not-so-obvious ones are the directory being used as the Environment.CurrentDirectory of another process (like Explorer), being monitored by FileSystemWatcher and being scanned by anti-malware or search indexer program. Beyond delay-and-try-again code, consider that moving it to the recycle bin almost always works. – Hans Passant Dec 01 '17 at 17:16
  • Agree with Hans, `GetLastWriteTime` appears to call directly down to the Win32 API so I doubt it's locking the directory itself, but it could be kicking off the AV/Malware process for example. – DavidG Dec 01 '17 at 17:23
  • @HansPassant Appreciate the input. Some sort of File Watch or scan was my next guess, but seems like it will be a nightmare to debug. – Talen Kylon Dec 01 '17 at 17:25

0 Answers0