delphi目录存在网络映射单元的函数奇怪行为

标签 delphi function directory delphi-xe exists

在delphi XE中,当我使用以下输入调用SysUtils DirectoryExists函数时

'Y:\blabla\'

其中 Y 是网络映射单元,它正确返回 false,因为 blabla 不存在。

但是当我使用以下输入调用时

'Y:\blabla\Y:\bla'

它返回 true。

文档很差,我在互联网上没有找到遇到同样问题的人

也许这里有人已经遇到了这个问题,或者知道发生了什么?

最佳答案

DirectoryExists 的实现中似乎存在一个错误功能。

这是该函数的相关代码

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
    ...
    //more code
    ...
  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}

如您所见,如果 GetFileAttributes函数调用失败,结果为GetLastError方法与一组可能的值进行比较。但在您的情况下,传递无效路径将返回 ERROR_BAD_PATHNAME (161) 代码,因此该函数返回 True。

关于delphi目录存在网络映射单元的函数奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15166420/

相关文章:

javascript - IE10 Javascript "Function Expected"

python - Delphi:模拟按键以实现自动化

c++ - 我想在 Delphi/WIN32 中使用 Infocardapi.dll 但想要它的头文件

delphi - FireDac FDQuery 在浏览结果时提交更改的数据字段

r - 如何修改R本地环境中预先存在的函数

python - 如何从一条路径获取上层路径?

delphi - 如何混合套接字,消息和事件

javascript - 关于javascript函数引用的问题

python : getcwd and pwd if directory is a symbolic link give different results

directory - 选择后添加浏览安装目录 "${PRODUCT_NAME}"后的 NSIS 目录页面