如何从Delphi中的绝对路径获取文件URL?

时间:2011-12-13 23:49:22

标签: delphi file url path delphi-2009

我正在寻找一个Delphi函数,它从Windows路径返回文件URL路径。 Delphi里面有内置的东西吗?

示例:

Input
C:\Users\Documents\File.txt

Output
file:///C:/Users/Documents/File.txt

由于

2 个答案:

答案 0 :(得分:8)

看看UrlCreateFromPath()。但请注意,有file:方案的警告。它不是跨平台的标准。有多种格式以不同的方式表示相同的路径,即使只是在Windows下。从IE4开始,Win32 API标准化为单一格式,但其他格式仍然存在。

答案 1 :(得分:7)

您可以使用UrlCreateFromPath API函数。

以下是示例:

uses
  ComObj, WinInet, ShLwApi;

function FilePathToURL(const FilePath: string): string;
var
  BufferLen: DWORD;
begin
  BufferLen := INTERNET_MAX_URL_LENGTH;
  SetLength(Result, BufferLen);
  OleCheck(UrlCreateFromPath(PChar(FilePath), PChar(Result), @BufferLen, 0));
  SetLength(Result, BufferLen);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(FilePathToURL('C:\Users\Documents\File.txt'));
end;