如何使用Delphi检查路径是否指向根文件夹

deo*_*nvn 6 delphi

检查特定路径是否指向驱动器根目录的最佳/最简单方法是什么?

我想我可以检查路径名是否以'\'或':'结尾,或者路径长度只有2或3个字符,但我希望有一些标准的"IsDriveRoot"功能来检查这个.

TX

更新:

在搜索Delphi帮助文件后,我找到了ExtractFileDrive()函数,它返回任何给定路径的驱动器部分.

使用该函数我认为很容易编写一个小函数来检查原始路径是否与ExtractFileDrive()的结果相同,这意味着原始路径必须是驱动器的根.

Function IsDriveRoot(APath: string): Boolean;
begin
  Result := ((Length(APath) = 2) and (ExtractFileDrive(APath) = APath))
         or ((Length(APath) = 3) and ((ExtractFileDrive(APath) + '\') = APath));
end;
Run Code Online (Sandbox Code Playgroud)

要么

Function IsDriveRoot(APath: string): Boolean;
begin
  Result := ((Length(APath) = 2) and (Copy(APath,2,1) = ':'))
         or ((Length(APath) = 3) and (Copy(APath,3,1) = '\'));
end;
Run Code Online (Sandbox Code Playgroud)

这样的东西应该做到....

我实际上认为第二个例子更简单,并可能最终使用那个.

再次感谢所有回复的人:)

bar*_*ddu 0

您可以利用 GetDriveType() 调用:

if GetDriveType(PChar(path)) <> DRIVE_NO_ROOT_DIR then
...
Run Code Online (Sandbox Code Playgroud)