How to get the file URL from absolute path in Delphi?

主宰稳场 提交于 2019-12-12 11:14:18

问题


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


回答1:


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;



回答2:


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.



来源:https://stackoverflow.com/questions/8497895/how-to-get-the-file-url-from-absolute-path-in-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!