# [SOLVED] Determine if a file is already open by another users



## Focus2 (Nov 30, 2011)

Im doing alittle coding. In Im trying to find out how to determine if a file is open using a visual basic proggram. Basically I trying to see if the file in the directory is already open from a user.

Thanks in Advance


----------



## AlbertMC2 (Jul 15, 2010)

*Re: Determine if a file is already open by another users*

Hi

Don't know if this is helpful but you can try it:
VB.NET - Check if file is in use


----------



## Focus2 (Nov 30, 2011)

*Re: Determine if a file is already open by another users*

It didnt work, but thanks for the Help.


----------



## Ninjaboi (Dec 27, 2010)

*Re: Determine if a file is already open by another users*

This might work for you:


```
'Determine whether a file is already open or not
Private Declare Function lOpen Lib "kernel32" Alias "_lopen" (ByVal lpPathName As String, ByVal iReadWrite As Long) As Long
Private Declare Function lClose Lib "kernel32" Alias "_lclose" (ByVal hFile As Long) As Long
Private Function IsFileAlreadyOpen(FileName As String) As Boolean
    Dim hFile As Long
    Dim lastErr As Long
    ' Initialize file handle and error variable.
    hFile = -1
    lastErr = 0
    ' Open for for read and exclusive sharing.
    hFile = lOpen(FileName, &H10)
    ' If we couldn't open the file, get the last error.
    If hFile = -1 Then
        lastErr = Err.LastDllError
    Else
        ' Make sure we close the file on success.
        lClose (hFile)
    End If
    ' Check for sharing violation error.
    sFileAlreadyOpen = (hFile = -1) And (lastErr = 32)
End Function
Private Sub Form_Load()
    'example by Matthew Gates ([email protected])
    MsgBox IsFileAlreadyOpen("c:\autoexec.bat")
End Sub
```
*Source(s):*

Check if file is open in vb - Dev Shed


----------



## Focus2 (Nov 30, 2011)

*Re: Determine if a file is already open by another users*

Thanks, that did the job. Your help is appreciated.


----------



## Ninjaboi (Dec 27, 2010)

*Re: Determine if a file is already open by another users*

Glad it worked. Marking thread "Solved".

Happy coding!


----------

