Search code examples
delphiwindows-10escapingmicrosoft-edge

Open local file in Edge from Delphi program


I have following procedure that should invoke Edge from Delphi program and open local file which address is given in sFileName

procedure OpenFileInEdge (
  const Handle:HWND;
  const sFileName: string); 

begin
  ShellExecute (Handle, 'open', Pchar('"shell:Appsfolder\Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge"'),
                Pchar ('"' + sFileName + '"'), nil, sw_ShowNormal);
end;

it works fine, unless there is a space somewhere in the file name itself or name of any of the parent maps. When there is a space, for example "E:\Temp\File Name.html" Edge would instead of one tab with the specified file name open two tabs, one with name "E:\Temp\File" and another with "Name.html".

When I drag file from Win Explorer to Edge it shows address as "E:\Temp\File%20Name.html". So, I have changed the call in above procedure to this:

                Pchar (StringReplace (sFileName, ' ', '%20', [rfReplaceAll])), nil, sw_ShowNormal);

And now Edge open one tab with the following address: "E:\Temp\File%2520Name.html"

I have checked that after replacement file name is indeed "E:\Temp\File%20Name.html", so somewhere along the way from Delphi to Edge it is changed to "E:\Temp\File%2520Name.html".

OK, 25 is hex code for %, so how can I tell Windows not to convert % to %25 ?

I have found several suggestions what to do and I have tried:

  • putting file name in single quotes - same result as double quotes
  • putting different escape characters in front of %: % /% ^% %% &%
  • putting % in quotes: "%"

Of course


Solution

  • Adding "file:\" prefix didn't change unwanted behavior, nor any change from blank to %20, also I didn't want to open document in default browser but specifically in Edge (default is Chrome).

    What did help is not adding another pair of double quotes, but another two pairs of double quotes. Working procedure now looks like this:

    procedure OpenFileInEdge (
      const Handle:HWND;
      const sFileName: string);
    
    begin
      ShellExecute (Handle, 'open', Pchar('"shell:Appsfolder\Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge"'),
                    Pchar ('"""' + sFileName + '"""'), nil, sw_ShowNormal);
    end;