开发者

How to check if path points to a root folder using Delphi

What is the best/easiest way to check if a certain path points to a drive's root?

I guess I could just check if path name ends with '\' or ':', or if the path is only 2 or three characters in length, but I was hoping there was some kind of standard "IsDriveRoot" function to check this.

Tx

UPDATE:

After searching through the Delphi help file I found the ExtractFileDrive() function which returns the drive portion of any given path.

Using that function I gues it's easy to write a little function to check if the original path is the same as the result of ExtractFileDrive(), which would mean that the original path had to be the drive's root.

Function IsDriveRoo开发者_JS百科t(APath: string): Boolean;
begin
  Result := ((Length(APath) = 2) and (ExtractFileDrive(APath) = APath))
         or ((Length(APath) = 3) and ((ExtractFileDrive(APath) + '\') = APath));
end;

or

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;

Something like that should do it....

I actually think the second example is simpler, and will probably end up using that one.

Thanks again to all who responded :)


It seems GetVolumePathName can be quite helpful in your case.


You could utilize GetDriveType() call:

if GetDriveType(PChar(path)) <> DRIVE_NO_ROOT_DIR then
...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜