How to check if file is in use (Windows)
gregorK
I have a question regarding how to check if a file is in use by another program, before trying to open it for reading in data with IGOR? I have checked the
WaitForFileProcs.ipf
procedure file and the WaitForWriteAccesToFile
function defined there which will (I think) not work in my case, because I am trying to read from a remote machine where I do not have write access and therefore the trick in the function with trying to append to file will always fail.Are there any other options, tricks?
Thanks a lot!
Gregor
Not that I know of. There may be some low-level way, using the Win32 API, to check if a file is open but I am not aware of it.
Other than that you would need the cooperation of the software on the other machine that is opening the file.
October 10, 2013 at 06:13 am - Permalink
GetFileFolderInfo
operation provides a lot of info on a disk file, unfortunately, none of it directly answers your question. Maybe you can use something such as the returned valueV_modificationDate
to get an indirect answer?October 10, 2013 at 06:53 am - Permalink
I briefly checked the Win32 API and the MSDN pages and it really seems that there is no direct way to check if a file is in use.
I will try this, probably using the file size information.
Thank you both for your quick response and help!
Cheers,
Gregor
October 11, 2013 at 05:20 am - Permalink
Shouldn't LockFileEx[1] with LOCKFILE_EXCLUSIVE_LOCK set accomplish that?
As far as I understand it, you will only get an exclusive lock if no other process has already locked the file.
[1]: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365203%28v=vs…
October 11, 2013 at 05:42 am - Permalink