Einzelnen Beitrag anzeigen

shmia

Registriert seit: 2. Mär 2004
5.508 Beiträge
 
Delphi 5 Professional
 
#2

AW: Lock File but My app still can read it?

  Alt 10. Feb 2011, 16:12
There are two different ways in window to lock files or parts of files.
1.) file locking
While a file is opened it is protected against deleting.
Windows doesn't allow to delete a file while it is opened by one or more processes.
2.) record locking
With LockFile() and UnlockFile() you can lock a range of bytes to prevent an other process to read or write this part of the file.

Delphi-Quellcode:
var
  fs : TFileStream;
  s : Ansistring;
begin
  fs := TFileStream.Create(filename, fmOpenRead or fmShareDenyWrite);
  try
    // File is open now and can't be deleted
    ...
    // read some data from file
    SetLength(s, 100);
    fs.ReadBuffer(s[1], 100);

    ....

  finally
    fs.free; // close file handle and free memory for FileStream object
  end;
end;
Andreas
  Mit Zitat antworten Zitat