You can try for a WinAPI function. This first link tries to open the file exclusively and if it fails, gives you a sharing error. The routine in question then returns either a true or a false Boolean for whether the file is open.
I am using API function to move files from multiple folder to one folder. B'coz file can be dropped constantly in their folders. How can i check if the file is open or not.. Thanks in advance
www.vbforums.com
You could also try to directly lock the file exclusively. Again, if it is open, you would get a sharing error because you can't take out an exclusive lock on an already opened file.
Locks the specified file for exclusive access by the calling process. This function can operate either synchronously or asynchronously and can request either an exclusive or a shared lock.
learn.microsoft.com
A file has a set of usage locks associated with it. These usage locks can be "interest locks" (the mildest lock there is) all the way up to "exclusive write lock" (the strongest lock there is.) If you have the ability to connect to the file's lock information, you could determine how many users have taken out locks. If 0, the file should be free. The problem with this statement is that you have to be privileged in order to get to any information that would identify the other users of the file. (I.e. Run as Admin)
Note that for the two links, (a) either method needs to immediately be reversed if successful (so that YOU don't leave the file locked for others) and (b) if the file is not in use but its lock list has not been correctly reset due to any kind of network glitch or user procedural error, you will get a false positive indication that the file is in use. The only solutions for a false positive are to reboot the system that holds the file OR to wait for a system-wide timeout that causes idle locks to go away OR use admin privileges to try to manually unlock the file. The lock timeout depends on your system admin's choices for the corresponding system setting. The manual unlocking method requires you to be an Admin. The reboot can be done by anyone who has access to the power cord, but doing a brutal power shutdown has its own penalties from users who might come after you with pitchforks and lit torches.