检查特定路径是否指向驱动器根目录的最佳/最简单方法是什么?
我想我可以检查路径名是否以'\'或':'结尾,或者路径长度只有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;
或
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;
这样的事情应该做到....
我实际上认为第二个例子更简单,并且可能最终会使用那个。
再次感谢所有回复的人:)
答案 0 :(得分:3)
似乎GetVolumePathName
对您的情况非常有帮助。
答案 1 :(得分:0)
您可以使用GetDriveType()调用:
if GetDriveType(PChar(path)) <> DRIVE_NO_ROOT_DIR then
...