Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to instantly check whether a directory is in use?

I want to move a directory and all of its subdirectories with Directory.Move.

Before I do that, however, I want to check whether any of the files and subfiles in the directory and its subdirectories are being used by other processes.

Then, before the move, I'd like to lock the directory to other processes, so I can be sure that Directory.Move won't throw any exceptions.

What is the best way to accomplish that?

I would like to avoid checking for the usage of the individual files file by file, because the fact that the file isn't used when the software checks for it does not mean that it won't be used when the movement process starts.

like image 592
Arsen Zahray Avatar asked Jan 25 '12 13:01

Arsen Zahray


People also ask

How do I check if a directory is empty in C++?

We can call IsEmpty to check whether a given directory is empty. An empty directory is considered to have no files or other directories in it. IsEmpty returns true if the directory is empty; false otherwise.

How can I tell if a Windows file is locked?

In the Resource Monitor window, go to the CPU tab and expand the Associated Handles option. Now, in the search box, type the name of the file that is showing locked by a process and press Enter button. It will show you a list of processes holding the target file.


1 Answers

There is no way to lock the folder (+ sub folders) so you'll always end up with a race condition and there is no guarantee that there will be no exception.

There is always the possibility that something changes in between the check and the move so things can go wrong.

Just try to move the folder and retry later if not succeeding.

See also: Delphi: Check whether file is in use (this is a similar question, just ignore the Delphi part)

like image 194
Emond Avatar answered Oct 16 '22 18:10

Emond