How to get the file URL from absolute path in Delp

2019-06-21 19:49发布

I'm looking for a Delphi function which returns the file URL path from the Windows path. Is there something for it built-in in Delphi ?

Example:

Input
C:\Users\Documents\File.txt

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

Thanks

2条回答
Rolldiameter
2楼-- · 2019-06-21 20:14

You can use the UrlCreateFromPath API function.

Here is the example:

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;
查看更多
闹够了就滚
3楼-- · 2019-06-21 20:18

Look at UrlCreateFromPath(). Note that there are caveats with the file: scheme, though. It is not stanardized across platforms. There are multiple formats to represent the same path in different ways, even just under Windows. Since IE4, the Win32 API standardizes on a single format, but other formats still exist.

查看更多
登录 后发表回答