相対パスを絶対パスに変換する
"..\..\bin" のような記述のパスを絶対パスへ変換するには ExpandFileName 関数を使います。 ExtractRelativePath とはちょっと使い方が違うので注意です。 ExpandFileNameを呼び出す前にベースとなるパスをカレントディレクトリにセットしなければいけません。カレントディレクトリは SetCurrentDir 関数を使います。
| ■ ExpandFileNameの使用例 |
procedure TForm1.Button2Click(Sender: TObject);
var
Path1 : string;
Path2 : string;
begin
Path1 := 'C:\Program Files\Borland\Delphi5\lib';
Path2 := '..\..\..\Common Files\Borland Shared\BDE\';
if SetCurrentDir(Path1) then begin
//結果= C:\Program Files\Common Files\Borland Shared\BDE
Label1.Caption := ExpandFileName(Path2);
end;
end;
|
逆は ExtractRelativePath 関数を使います。