如何从Delphi中的绝对路径获取文件URL? [英] How to get the file URL from absolute path in Delphi?

查看:325
本文介绍了如何从Delphi中的绝对路径获取文件URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个Delphi函数,该函数从Windows路径返回文件URL路径.在Delphi中有内置的功能吗?

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 ?

示例:

Input
C:\Users\Documents\File.txt

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

谢谢

推荐答案

您可以使用

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;

这篇关于如何从Delphi中的绝对路径获取文件URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆