function FileExists2(const FileName: string): Boolean;
function ExistsLockedOrShared(const Filename: string): Boolean;
var
FindData: TWin32FindData;
LHandle: THandle;
begin
LHandle := FindFirstFile(PChar(Filename), FindData);
if LHandle <> INVALID_HANDLE_VALUE then
begin
Windows.FindClose(LHandle);
Result := FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY = 0;
end
else
Result := False;
end;
var
Code: Integer;
LastError: Cardinal;
OldMode: Cardinal;
begin
OldMode := SetErrorMode(SEM_FAILCRITICALERRORS);
Code := Integer(GetFileAttributes(PChar(FileName)));
if Code <> -1 then
Result := (FILE_ATTRIBUTE_DIRECTORY and Code = 0)
else
begin
LastError := GetLastError;
Result := (LastError <> ERROR_FILE_NOT_FOUND) and
(LastError <> ERROR_PATH_NOT_FOUND) and
(LastError <> ERROR_INVALID_NAME) and ExistsLockedOrShared(Filename);
end;
SetErrorMode(OldMode);
end;