views:

39

answers:

1

I need a programmatic way to know if a directory (as opposed to a file) is in use, for example because it is open on explorer or a CMD prompt. If the directory is in use, then it cannot be deleted. The current way I have found to do that is trying to rename it, is there a less intrusive way to do this under Windows?

A: 

You didn't secify a language, so I am assumming c++. You can try to lock the file yourself using LockFile or LockFileEx, if it returns zero another process has the file locked already.

You can't unlock a lock if another process has it locked without terminating the process.

Byron Whitlock
You assumed C++, but then you never used that assumption! :)
Andreas Rejbrand
@Andreas, ha, I guess since the linked function is C you got me there ;)
Byron Whitlock
@Byron: Well, most importantly, `LockFile` is a function of the Windows API (i.e., technically an exported function of a DLL), and so it can be called from *any* programming language that is able to call functions in DLLs, which is more or less any "real" programming language. Myself I use Delphi to call the Win32 functions.
Andreas Rejbrand