delphi 中相对路径与绝对路径函数说明  
官方Delphi 学习QQ群: 682628230(三千人)
频道

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;


推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.049704074859619 seconds