How to start default browser with a URL(with spaces and #) for a local file? Delphi

后端 未结 6 1303
天涯浪人
天涯浪人 2021-01-21 06:15

I want to open a local HTML file in the default browser.

For example: Default Browser is Mozilla Firefox.

The file to be opened: C:\\My Custom Path\\New Folde

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-21 06:48

    Unfortunately you can't do it only with ShellExecute. But there is some hack.

    For example you want to open url like this:

    C:\Users\User\Desktop\Some sitename with spaces.htm with the anchor #myAnchor

    To make ShellExecute open file:

    vS := 'file:///C:\Users\User\Desktop\Some sitename with spaces.htm#MyAnchor';
    ShellExecute(0, 'OPEN', PChar(vS), '', '', SW_SHOWNORMAL);
    

    It's Important to use "file:///" in the beginning, but ShellExecute open this page without anchor.

    To open with the anchor you can dynamically create html file with content like this:

    
    
    
    
    

    And open this dynamic file with ShellExecute.

    Hope it helps.

提交回复
热议问题