判断文件FileName是否正在被打开/使用,如果被占用返回true,否则返回false
function IsFileInUse(const FileName: string): boolean;
var
HFileRes: HFILE;
begin
if not FileExists(FileName) then
begin
Result := False;
Exit;
end;
try
HFileRes := CreateFile(pchar(FileName), GENERIC_READ,
0 {this is the trick!}, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
Result := (HFileRes = INVALID_HANDLE_VALUE);
if not Result then
CloseHandle(HFileRes);
except
Result := true;
end;
end;