Solved

Check file lock

Posted on 2003-11-09
3
317 Views
Last Modified: 2010-05-01
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
Comment
Question by:MarcGraff
3 Comments
 
LVL 24

Accepted Solution

by:
R_Rajesh earned 250 total points
ID: 9710753
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
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 9711324
0
 
LVL 1

Author Comment

by:MarcGraff
ID: 9711914
Thank you so much!

   - Marc
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now