How to determine if a File is opened by another process?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want to know if a file is currently locked aby nother prcess... but in the
most efficent way possible.. i mean is there some API calls that i can use? i
would want to avoid having to catch (and cause) exceptions since they are
really slooooooow..

i know the following works:
Private Function IsOpen(ByVal sFilePath As String) As Boolean
Dim boOut As Boolean
Try
Dim oFileStream As New IO.FileStream(sFilePath,
IO.FileMode.Open, IO.FileAccess.ReadWrite, IO.FileShare.None)
oFileStream.Close()
boOut = False
Catch
boOut = True
End Try
Return boOut
End Function

But is there a better way (withotu Exceptions)? i would like something like
IO.File.IsLocked("filePath") ... (if that existed)..

How does windows determine if a file is opened/locked/in use? Is there a way
to make the same check through api?

Thanks for any help!!!
 
I do not believe there is a better way. I've looked for one, and I've never
seen anyone that has demonstrated one. This is one of those cases where
using Try/Catch is not only a good idea, but indispensible.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but you can't make it stink.
 
There is a way how you can do it:

- write file system driver that attaches filter device objects to target
file system device objects
- make P/Invoke to DeviceIOControl to communicate with your driver and
receive data...

So using try/catch will be more efficient and much cheaper way :8-)
 
Back
Top