打开并锁定一个文件(使用LockFile API函数)

var
  aHandle     : THandle;
  aFileSize   : Integer;
  aFileName   : String;

procedure TForm1.Button3Click(Sender: TObject);
begin
    aFileName    := 'C:\101\Java_new.pdf';
    aHandle      := CreateFile(PChar(aFileName),GENERIC_READ, 0, nil, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0); // get the handle of the file

    aFileSize   := GetFileSize(aHandle,nil); //get the file size for use in the  lockfile function
    Win32Check(LockFile(aHandle,0,0,aFileSize,0)); //lock the file
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
    Win32Check(UnlockFile(aHandle,0,0,aFileSize,0));//unlock the file
    CloseHandle(aHandle);//Close the handle of the file.
end;

 

http://stackoverflow.com/questions/1916084/how-do-i-get-the-handle-for-locking-a-file-in-delphi

posted @ 2016-09-26 17:54  findumars  Views(2550)  Comments(0Edit  收藏  举报