delphi 判断文件是否被占用

class function TWmsApplicationfunction.IsFileInUse(FileName: TFileName): Boolean;
var
  HFileRes: HFILE;
begin
  Result := False;
  if not FileExists(FileName) then
    Exit;    // 如果文件不存在,返回false
  HFileRes := CreateFile(PChar(FileName), GENERIC_READ or GENERIC_WRITE, 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  Result := (HFileRes = INVALID_HANDLE_VALUE);
  if not Result then
    CloseHandle(HFileRes);
end;

 

posted @ 2017-12-13 11:28  夏天的西瓜君  阅读(848)  评论(0编辑  收藏  举报