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)
*)

相关阅读 >>

Delphi中的copy,delete,pos和leftstr,rightstr的用法

Delphi 得到一个cuid用户唯一标识

Delphi 使用indy解决tidssliohandlersocketopenssl could not load ssl library错误

Delphi xe 在andriod程序中获取外置sd卡根目录

Delphi 网上获取北京时间 tinifile.readsection 方法在 android 下的应用及各种字符编码问题

Delphi查找程序坐标 获取鼠标 模拟鼠标

Delphi 在tedit中显示水印提示

Delphi 取得当前进程占用内存及线程数

Delphi 获取所有应用程序窗口标题 类似任务管理器

Delphi 根据关键字取关键词以后的字符串

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



打赏

取消

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

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

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

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

评论

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