Question : How detect if the file being used by another process

Is there anyway to detect and know if the file being used by another process? To avoid opening this file and wait until the other process finish!

Answer : How detect if the file being used by another process

The syntax error is easily fixed by
...OpenFile(PAnsiChar(FileName), ..
but it doesn't actually work.

This one does.

function FileInUse(FileName: string): Boolean;
var hFileRes: HFILE;
begin
  Result := False;
  if not FileExists(FileName) then exit;
  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;
Random Solutions  
 
programming4us programming4us