You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何展开含\..\的路径?求Delphi或WinAPI相关函数

路径展开的Delphi/WinAPI解决方案

你可以用以下几种方式实现路径的展开,处理其中的..父目录引用:

WinAPI函数:GetFullPathName

这是Windows原生API,直接调用即可解析路径:

var
  szBuffer: array[0..MAX_PATH] of Char;
begin
  GetFullPathName('c:\dev\myapp\dirA\..\dirB\myfile.txt', MAX_PATH, szBuffer, nil);
  // szBuffer 中会返回展开后的路径:c:\dev\myapp\dirB\myfile.txt
end;

Delphi VCL函数:ExpandFileName

VCL提供的封装函数,使用起来更简洁,无需手动处理缓冲区:

var
  sExpandedPath: string;
begin
  sExpandedPath := ExpandFileName('c:\dev\myapp\dirA\..\dirB\myfile.txt');
  // sExpandedPath 的值为 c:\dev\myapp\dirB\myfile.txt
end;

Delphi 现代函数:TPath.GetFullPath(XE2及以上版本)

如果使用Delphi XE2或更高版本,推荐使用System.IOUtils单元中的TPath类方法,这是更符合现代Delphi风格的实现:

uses
  System.IOUtils;

var
  sExpandedPath: string;
begin
  sExpandedPath := TPath.GetFullPath('c:\dev\myapp\dirA\..\dirB\myfile.txt');
end;

以上三种方式都能正确处理路径中的..、.等相对引用,生成规范的绝对路径。

内容的提问来源于stack exchange,提问作者zeus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 20:15:32