Delphi 相对路径和绝对路径的转换

(*
相对路径和绝对路径的转换
*)


unit xPath;


interface
  uses ShlwApi, Windows, SysUtils;


/// <summary>
/// 取绝对路径的函数。需要引用 ShlwApi.pas
/// </summary>
/// <param name="BasePath">参考路径</param>
/// <param name="RelativePath">相对路径</param>
/// <code>
/// S := GetAbsolutePath('C:\Windows\System32', '..\DEMO.TXT')
//  S 将得到 'C:\Windows\DEMO.TXT
/// </code>
function GetAbsolutePathEx(BasePath, RelativePath:string):string;


/// <summary>
/// 取得本身程序的路径
/// </summary>
function AppPath : string;
function GetRelativePath(const Path, AFile: string): string;


implementation
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;


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;






function AppPath : string;
begin
  Result := extractFilePath(Paramstr(0));
end;
end.






(*
ExpandFileName() 返回文件的全路径(含驱动器、路径)
ExtractFileExt() 从文件名中抽取扩展名
ExtractFileName() 从文件名中抽取不含路径的文件名
ExtractFilePath() 从文件名中抽取路径名
ExtractFileDir() 从文件名中抽取目录名
ExtractFileDrive() 从文件名中抽取驱动器名
ChangeFileExt() 改变文件的扩展名
ExpandUNCFileName() 返回含有网络驱动器的文件全路径
ExtractRelativePath() 从文件名中抽取相对路径信息
ExtractShortPathName() 把文件名转化为DOS的8·3格式
MatchesMask() 检查文件是否与指定的文件名格式匹配
ExtractFilePath(FileName:String)
该函数返回路径名,其结尾字符总是“\”


ExtractFileDir(FileName:String)


*)


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值