I need a way to check if a file is in use or not by another application. at the moment I am using:
Code:
oFile.Move (oFile.Path)
If Err = 70 Then
'the file is in use
Else
'the file is not in use
End if
This works but it is slow. Basically it tries to cut and paste the file into the directory it's actually in, so really the file goes nowhere, but if it is locked by another process then it will chuck out error 70 and then we know that it's in use.
I know that this is a crude way of doing it, so, is there another way to check if a file is in use which is alot faster?
But if the file is on a cd, can i still use the same method?? I want to check if a file on a CD is in use.. pliz help..
Welcome to the Forum.
It is general practice to start your own thread when you have a question instead of posting in one started by another member. If another one is related post a link to it in your own. Also, when posting (solutions) to existing threads, do check the date so as not to post/revive old/dead threads. This one is 2 years old. In fact, it was it's second birthday 2 days ago
You really don't need all that code making API calls:
Code:
Dim intFile As Integer
intFile = FreeFile(0)
On Error Resume Next
Open "example.txt" For Input Lock Read Write As #intFile
If Err Then
Label1.Caption = "In Use!"
Else
Label1.Caption = "Not In Use!"
End If
Close #intFile
There is a small flaw in this method.
What if the user doesn't have read/write permissions on the file? A "permisssion denied" error will be thrown.
I don't remember what error occurs if it is already locked (assuming the user has read/write permissions), but if these 2 errors have different numbers, maybe you could do a check.