function DirectoryExists(
const Directory:
string; FollowLink: Boolean = True): Boolean;
{$IFDEF MSWINDOWS}
var
Code: Cardinal;
Handle: THandle;
LastError: Cardinal;
begin
Result := False;
Code := GetFileAttributes(PChar(Directory));
if Code <> INVALID_FILE_ATTRIBUTES
then
begin
if faSymLink
and Code = 0
then
Result := faDirectory
and Code <> 0
else
begin
if FollowLink
then
begin
Handle := CreateFile(PChar(Directory), GENERIC_READ, FILE_SHARE_READ,
nil,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
if Handle <> INVALID_HANDLE_VALUE
then
begin
CloseHandle(
Handle);
Result := faDirectory
and Code <> 0;
end;
end
else if faDirectory
and Code <> 0
then
Result := True
else
begin
Handle := CreateFile(PChar(Directory), GENERIC_READ, FILE_SHARE_READ,
nil,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
if Handle <> INVALID_HANDLE_VALUE
then
begin
CloseHandle(
Handle);
Result := False;
end
else
Result := True;
end;
end;
end
else
begin
LastError := GetLastError;
Result := (LastError <> ERROR_FILE_NOT_FOUND)
and
(LastError <> ERROR_PATH_NOT_FOUND)
and
(LastError <> ERROR_INVALID_NAME)
and
(LastError <> ERROR_BAD_NETPATH);
end;
end;
{$ENDIF MSWINDOWS}