Link to home
Start Free TrialLog in
Avatar of ASP_Ray
ASP_RayFlag for United States of America

asked on

How to check if file is locked

Can anyone tell me how to check if a file is locked by an application for write access and how to check which user locked it?  Excel does this.  If you try to open a file already opened, it will tell you that the file is "locked for editing" by "...".

All I can do right now is to try to open the file and trap error number 70, "Permission denied".
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

As far as I know do the Office applications store the user into their files when opening the file...
could you use an error handler?
Avatar of ASP_Ray

ASKER

I'm not sure what the comments from angelll and Richie Simonetti mean so let me restate my question.  I want to know how, from my program, to check if a file is locked by another program because the other program is editing the file.  If I could find out as well which user is using the application which locked the file, that would be nice too.  If you want, say, to delete a file, you must do more than just check if the file exists to avoid a run-time error.  You must also check if the file is locked.  Right now I'm using a workaround to check if the file is locked.  I try to open the file with the VB's Open statement and then check for error number 70.  I do it as follows:

  On Error Resume Next
  iFileNumber = FreeFile
  Open sFileSpec For Input As iFileNumber
  If Err.Number = 70 Then
    'Permission to open is denied
    MsgBox Err.Description
  End If
  Close iFileNumber

Actually, in practice, you would check
"If Err.Number <> 0" but this code is to illustrate what I mean.  

My previous comment about Excel is this.  If you try to open an Excel file on a server, if that file is already opened anywhere on the network, Excel will tell you that the file is locked and will tell you which usere is using the application which locked the file.  Don't get hung up on Excel though, I just used this as an example of ultimately what I would like to do.

Hope this clarifies thinks.  Please continue to comment.
Hi ASP_Ray:
I did fully understand your problem, and even if you choose Excel as sample, it is a special case, as I know that Excel (as the other MS Office products) store the information of who has openend the file in the file itself, so that other Excel users can get notified of this when trying to open the same file.

This is not implemented for the mass of normal files, as no other application does store this information in their files...

So ultimately, you have to do the same way, either storing the information in the file itself or a secondary file besides... There is no way except checking the error as you showed, and using that method there is no way of telling who opened the file.

CHeers
Avatar of ASP_Ray

ASKER

Thanks Angellll.  I'm sorry about the delay in answering but my ISP, MSN, was down all day yesterday.  

Now just one last clarification.  You already addressed it but I just want to emphasize.  Are you saying there is no direct way to check if a file is locked?  No Win32 API function?  I can only try to open the file and check for errors, which doesn't even directly return that the file is locked, just a general "permission denied"?
ASKER CERTIFIED SOLUTION
Avatar of Richie_Simonetti
Richie_Simonetti
Flag of Argentina image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ASP_Ray

ASKER

Thanks for all the comments guys, especially you Angelll.  Wish I could split the points with you but since I can't, code always beat no code.  Anyway, keep up the good work guys.
Thanks for "A" grade but what could you use, Angel's suggestion or mine?, i don't understand.
Avatar of ASP_Ray

ASKER

Riche, Angel's suggestion was very informative.  It addressed that only office products could tell which user locked the file.  But my primary concern was how to check that file was locked (you can read the question again), and my method, it seemed to me at the time, was only only a work-around.  I wanted a definitive method to do that.  You provided code for this, and even more that, you provided an explanation that had the authority of Microsoft.  That is why I gave you the grade A.  I am now using the code you provided.

Actually I searched the MSDN library for hours on that subject, but I never searched for "sharing violation" or anything similer.  I was searching for text like "locked" etc. so I never came across that article.  Thanks again Richie and lots of thanks to Angellll as well.  Angellll taught me something I am glad to know and you gave me code I am now using.

PS.  I just re-read the last comment I made and I guess since it only spoke to Angelll specifically, you might have thought I was using his suggestion.  SORRY for the confusion.