2011-12-13 146 views

回答

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; 
8

UrlCreateFromPath()。請注意,雖然有file:方案的警告。它沒有跨平臺的標準化。有多種格式可以用不同的方式表示相同的路徑,即使在Windows下也是如此。自IE4以來,Win32 API在單一格式上標準化,但其他格式仍然存在。

+0

感謝您的回覆。關於文件方案,是否意味着UrlCreateFromPath函數的結果可能會有所不同,並取決於安裝的Interner Explorer?我希望目前沒有人有IE4,但我需要依靠這個功能。 – 2011-12-14 00:10:04

+1

UrlCreateFromPath()是在Win98SE和Win2K的IE5中引入的。微軟在IE4中標準化了對'file:`方案的使用。只要你將URL傳遞給Shell API,你應該沒問題。 – 2011-12-14 02:20:49

相關問題