Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / Win API / September 2003



Tip: Looking for answers? Try searching our database.

Test for "File in use"

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ian Bayly - 24 Sep 2003 01:16 GMT
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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.