Hi all,
I have a vb program that opens a file(.btw) if the input matches the .btw file name. How do i prevent from opening another .btw file via input if there's already one file opened? Please help me experts out there. Thanks.
Hi all,
I have a vb program that opens a file(.btw) if the input matches the .btw file name. How do i prevent from opening another .btw file via input if there's already one file opened? Please help me experts out there. Thanks.
I would try something like -
'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()
MsgBox IsFileAlreadyOpen("c:\autoexec.bat")
End Sub
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.