Newsgroups : Borland : borland.public.delphi.nativeapi.win32 : 2006 May : Problem to determine if file is locked.

www.cryer.info
Managed Newsgroup Archive

Problem to determine if file is locked.

Subject:Problem to determine if file is locked.
Posted by:"Ruediger Kabbasch" (ruediger.kabbas..@thermo.com)
Date:Tue, 2 May 2006 11:37:17 +0200

Hi everybody.

I use the following code to check if a file is opened by another user:

Function IsFileLocked(const fileName : String; openRead : Boolean = True;
openWrite : Boolean = True) : Boolean;
var
   chkStream     : TFileStream;
begin
    Try {[01]}
       Result := False;
       IF FileExists(fileName) Then
       begin
          IF (openRead = openWrite) Then
          begin
             chkStream := TFileStream.Create(fileName, fmOpenReadWrite OR
fmShareExclusive);
          end
          else begin
             IF openRead Then
             begin
                chkStream := TFileStream.Create(fileName, fmOpenRead OR
fmShareExclusive);
             end
             else begin
                chkStream := TFileStream.Create(fileName, fmOpenWrite OR
fmShareExclusive);
             end; {-> IF openRead Then }
          end; {-> IF (openRead = openWrite) Then }
          chkStream.Free;
       end; {<- IF FileExists(fileName) Then }
    except {[01]}
       Result := True;
       // chkStreamFree; Don't free the object here, that causes an error
    end; {[01]}
end;

Unfortunately this works not correct if I make the test on shared
directories.

The folder name is an UNC name.

If I copy from my test machine a file into the folder and check this file
the answer is as expected -> The file is not locked.

If another user on the destination machine copy a file into the folder and I
check the file from my test machine the result is wrong. I cannot open the
file exclusive allthough it is not opened by any user.

Any idea how to solve this?

Thanks in advance for any help.
Kind regards,

Ruediger Kabbasch

Glossary

Replies:

www.cryer.info
Managed Newsgroup Archive