Delphi 中相对路径与绝对路径函数说明


本文整理自网络,侵删。

 

以前也曾经为 Delphi 中相对路径与绝对路径之间的转换烦恼过。但时,用得少,也没过多地去注意了。今天要写一个小程序,又遇到了这个问题。就找资料吧,终于找到了最简单,最直接的方式。现在把函数整理如下:

需要引用 ShlwApi.pas

//取绝对路径的函数

// BasePath: 是参考路径。RelativePath: 是指相对路径

function GetAbsolutePathEx(BasePath, RelativePath:string):string;
var
Dest:array [0..MAX_PATH] of char;
begin
FillChar(Dest,MAX_PATH+1,0);
PathCombine(Dest,PChar(BasePath), PChar(RelativePath));
Result:=string(Dest);
end;


示范:

S := GetAbsolutePath('C:\Windows\System32', '..\DEMO.TXT')

// S 将得到 'C:\Windows\DEMO.TXT



//绝对路径转换为相对路径的函数


function GetRelativePath(const Path, AFile: string): string;

function GetAttr(IsDir: Boolean): DWORD;
begin
if IsDir then
Result := FILE_ATTRIBUTE_DIRECTORY
else
Result := FILE_ATTRIBUTE_NORMAL;
end;

var
p: array[0..MAX_PATH] of Char;
begin
PathRelativePathTo(p, PChar(Path), GetAttr(False), PChar(AFile), GetAttr(True));
Result := StrPas(p);
end;

相关阅读 >>

Delphi ini文件操作 tinifile、tmeminifile

Delphi10及以上版本安装activex控件

Delphi tstream详解

Delphi获取flash文件的影片时长,原始尺寸,帧数等信息

Delphi 全局变量 hinstance 到底是在什么时候赋值的?

Delphi中嵌入汇编检测是否在虚拟机里运行

Delphi数值转ip

Delphi 10.3.3 启动cnpack ide 专家 cnwizards coreide260.bpl错误解决办法

Delphi for 循环 to和downto的理解

Delphi idhttp上传图给asp完美解决

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...