Is there an API call which will test a file for 'in use'
(open)?
Thanks for any help
Ian B
Tom Esh - 24 Sep 2003 05:21 GMT
>Is there an API call which will test a file for 'in use'
>(open)?
Sure, but it won't be any more effective or simpler to implement than
just attempting to Open the file for exclusive (Lock Read Write)
access and trapping the error (55, IIRC) if it's already open.
Ex:
Public Function IsOpen(sFileName As String) As Boolean
Dim nFile As Long
On Error GoTo EH_Proc
nFile = FreeFile
'Attempt to get exclusive access.
'(Using input mode won't create new file if it doesn't exist.)
Open sFileName For Input Lock Read Write As nFile
Close nFile
Exit Function '==============>>>
EH_Proc:
If Err.Number = 55 Then
IsOpen = True
End If
End Function
-Tom
MVP - Visual Basic
(please post replies to the newsgroup)
Martin Wildam - 29 Sep 2003 22:08 GMT
Public Function IsFileAvailable(ByVal aFilename As String, Optional ByVal
ForWrite As Boolean = True, Optional ByVal ExclAccess As Boolean = True) As
Boolean
Dim f As Integer
On Error Resume Next
f = FreeFile
IsFileAvailable = False
If aFilename = "" Then Exit Function
Err.Clear
If ExclAccess Then
If ForWrite Then
Open aFilename For Binary Access Read Write Lock Read Write As #f
Else
Open aFilename For Binary Access Read Lock Read Write As #f
End If
Else
If ForWrite Then
Open aFilename For Binary Access Read Write Shared As #f
Else
Open aFilename For Binary Access Read Shared As #f
End If
End If
Close #f
If Err.Number = 0 Then
IsFileAvailable = True
End If
Err.Clear
End Function
> Is there an API call which will test a file for 'in use'
> (open)?
>
> Thanks for any help
>
> Ian B