Delphi中的绝对路径和相对路径之间的转换 [英] Conversion between absolute and relative paths in Delphi
问题描述
有没有标准的功能来执行Delphi中的绝对的相对路径转换?例如:
- 'Base'路径是
'C:\Projects\Project1\'
- 相对路径是
'.. \Shared\somefile.pas'
- 绝对路径是
'C:\Projects\Shared\somefile.pas'
我在找东西如下所示:
函数AbsToRel(const AbsPath,BasePath:string):string;
//'..\Shared\somefile.pas'=
// AbsToRel('C:\Projects\Shared\somefile.pas','C:\Projects\\ \\Project1\')
函数RelToAbs(const RelPath,BasePath:string):string;
//'C:\Projects\Shared\somefile.pas'=
// RelToAbs('.. \Shared\somefile.pas','C:\Projects\\ \\ Project1\')
我会使用 PathRelativePathTo
作为第一个功能,并 PathCanonizeize
作为第二个。在后一种情况下,作为参数,您传递基本路径和相对路径的字符串和。
函数PathRelativePathTo(pszPath: PChar; pszFrom:PChar; dwAttrFrom:DWORD;
pszTo:PChar; dwAtrTo:DWORD):LongBool;标准external'shlwapi.dll'name'PathRelativePathToW';
函数AbsToRel(const AbsPath,BasePath:string):string;
var
路径:数组[0..MAX_PATH-1]的char;
begin
PathRelativePathTo(@Path [0],PChar(BasePath),FILE_ATTRIBUTE_DIRECTORY,PChar(AbsPath),0);
result:= Path;
结束
函数PathCanonicalize(lpszDst:PChar; lpszSrc:PChar):LongBool;标准
external'shlwapi.dll'name'PathCanonicalizeW';
函数RelToAbs(const RelPath,BasePath:string):string;
var
Dst:array [0..MAX_PATH-1] of char;
begin
PathCanonicalize(@Dst [0],PChar(IncludeTrailingBackslash(BasePath)+ RelPath));
result:= Dst;
结束
程序TForm4.FormCreate(发件人:TObject);
begin
ShowMessage(AbsToRel('C:\Users\Andreas Rejbrand\Desktop\file.txt','C:\Users\Andreas Rejbrand\Pictures'));
ShowMessage(RelToAbs('.. \Videos\movie.wma','C:\Users\Andreas Rejbrand\Desktop'));
结束
当然,如果你使用非Unicode版本的Delphi(也就是< = Delphi 2007),您需要使用Ansi函数( * A
)而不是Unicode函数( * W
)。
Are there standard functions to perform absolute <--> relative path conversion in Delphi?
For example:
- 'Base' path is
'C:\Projects\Project1\'
- Relative path is
'..\Shared\somefile.pas'
- Absolute path is
'C:\Projects\Shared\somefile.pas'
I am looking for something like this:
function AbsToRel(const AbsPath, BasePath: string): string;
// '..\Shared\somefile.pas' =
// AbsToRel('C:\Projects\Shared\somefile.pas', 'C:\Projects\Project1\')
function RelToAbs(const RelPath, BasePath: string): string;
// 'C:\Projects\Shared\somefile.pas' =
// RelToAbs('..\Shared\somefile.pas', 'C:\Projects\Project1\')
I would use PathRelativePathTo
as the first function and PathCanonicalize
as the second. In the latter case, as argument you pass the string sum of the base path and the relative path.
function PathRelativePathTo(pszPath: PChar; pszFrom: PChar; dwAttrFrom: DWORD;
pszTo: PChar; dwAtrTo: DWORD): LongBool; stdcall; external 'shlwapi.dll' name 'PathRelativePathToW';
function AbsToRel(const AbsPath, BasePath: string): string;
var
Path: array[0..MAX_PATH-1] of char;
begin
PathRelativePathTo(@Path[0], PChar(BasePath), FILE_ATTRIBUTE_DIRECTORY, PChar(AbsPath), 0);
result := Path;
end;
function PathCanonicalize(lpszDst: PChar; lpszSrc: PChar): LongBool; stdcall;
external 'shlwapi.dll' name 'PathCanonicalizeW';
function RelToAbs(const RelPath, BasePath: string): string;
var
Dst: array[0..MAX_PATH-1] of char;
begin
PathCanonicalize(@Dst[0], PChar(IncludeTrailingBackslash(BasePath) + RelPath));
result := Dst;
end;
procedure TForm4.FormCreate(Sender: TObject);
begin
ShowMessage(AbsToRel('C:\Users\Andreas Rejbrand\Desktop\file.txt', 'C:\Users\Andreas Rejbrand\Pictures'));
ShowMessage(RelToAbs('..\Videos\movie.wma', 'C:\Users\Andreas Rejbrand\Desktop'));
end;
Of course, if you use a non-Unicode version of Delphi (that is, <= Delphi 2007), you need to use the Ansi functions (*A
) instead of the Unicode functions (*W
).
这篇关于Delphi中的绝对路径和相对路径之间的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!