问题
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