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);// <= Die entscheidende Zeile, die fehlte...
end;
end;
{$ENDIF MSWINDOWS}
{$IFDEF POSIX}
var
StatBuf, LStatBuf: _stat;
LDirectory: UTF8String;
Success: Boolean;
begin
LDirectory := UTF8Encode(Directory);
Success := stat(PAnsiChar(LDirectory), StatBuf) = 0;
Result := Success and S_ISDIR(StatBuf.st_mode);
if not Result and (lstat(PAnsiChar(LDirectory), LStatBuf) = 0) and
S_ISLNK(LStatBuf.st_mode) then
begin
if Success then
Result := S_ISDIR(StatBuf.st_mode)
else if not FollowLink then
Result := True;
end;
end;
{$ENDIF POSIX}