Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 327
  • Last Modified:

Check file lock

How could one check to see if a file is in use by another user. Example: I need to delete a file but before I do, check to see if it’s in use.

Is this possible?

   - Marc
0
MarcGraff
Asked:
MarcGraff
1 Solution
 
R_RajeshCommented:
try this,

Private Sub Form_Load()
Dim strFileName As String
' Full path and name of file.
strFileName = "C:\calc.exe"
' Call function to test file lock.
If Not FileLocked(strFileName) Then
' If the function returns False, file in not in use
MsgBox "not open, can delete it"
Else
MsgBox "file is open, cant delete it"
End If
End Sub

Function FileLocked(strFileName As String) As Boolean
On Error Resume Next
' If the file is already opened by another process,
' and the specified type of access is not allowed,
' the Open operation fails and an error occurs.
Open strFileName For Binary Access Read Write Lock Read Write As #1
Close #1
' If an error occurs, the document is currently open.
If Err.Number <> 0 Then
' Display the error number and description.
'MsgBox "Error #" & Str(Err.Number) & " - " & Err.Description
FileLocked = True
Err.Clear
End If
End Function
0
 
MarcGraffAuthor Commented:
Thank you so much!

   - Marc
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now