37

Since time immemorial, most web browsers have been able to open a local file if you ran the web-browser executable, for example just execute iexplore.exe file:/c:/temp/file or via the IShellDocView interfaces. I am trying to do this from within my own program, in Windows 10, with Microsoft Edge, and am unaware of how to do it.

The executable appears to be completely undocumented, does not respond to /? or /help, and simply crashes no matter what I pass to it, and given that the path appears to be likely to change, is probably not the correct approach to invoke this executable directly:

  C:\Windows\SystemApps\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\MicrosoftEdge.exe  <whatever>

Is there an API in Windows that can be invoked instead, that will open Edge, perhaps even if it is not the current default browser?

If it was the default browser, I believe I could just do what I want via Win32 shell-API ShellExecute. I would like to be able to launch something in Edge even if I have set another browser as my default though, for the purpose of automating certain web-testing tasks.

Are there programmatic interfaces or APIs for Edge? For purposes of this question, let's say I want to write this in C, but this should be the same API no matter what language I'm using so I didn't tag this question C.

If there is no way to do it programmatically, is there a command line argument I could use and pass to a MicrosoftEdge or MicrosoftEdgeCP executable?

UPDATE: In 2022, with the latest Chromium based Edge browser this problem is no longer a problem.

Warren P
  • 65,725
  • 40
  • 181
  • 316
  • related: http://stackoverflow.com/questions/31794639/how-to-add-edge-as-my-debug-browser-in-visual-studio-2012-on-windows-10 – Warren P Jan 14 '16 at 19:57
  • 2
    I have a vague idea maybe you have to use LaunchWinApp.exe to launch Edge, even though it looks like a Windows desktop app, it's really more like a Store app. – Warren P Jan 14 '16 at 19:58
  • 1
    Some sample code found in C++ https://github.com/MicrosoftEdge/MicrosoftEdgeLauncher – Warren P Jan 14 '16 at 19:58
  • At least today, it works when the file is saved locally, but fails when on a UNC path. – Nathan Hartley Oct 19 '16 at 18:53
  • Oh that does work. `MicrosoftEdgeLauncher.exe file:///C:/something/index.html` worked! – Warren P Oct 19 '16 at 19:42
  • @WarrenP that also does not work anymore. It seems that only `http` and `https` protocols are supported today. :/ – Martin Schneider Jul 02 '19 at 13:46
  • 1
    MS has now updated it. See my latest answer below. – www-0av-Com Jun 02 '20 at 10:55
  • @www-0av-Com it does not work from powershell, maybe from cmd. – Timo Aug 12 '20 at 10:38
  • 1
    @Timo, yep my simple solution at bottom of this question defo still works. See my comment below for more tips. – www-0av-Com Aug 13 '20 at 13:12
  • 1
    @www-0av-Com this now works, plus one for you: `& "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" file:///C:\Users\an-w-koernet\Documents\1.pdf` -- absolute path, not relative! – Timo Aug 14 '20 at 07:17
  • See [this answer](https://stackoverflow.com/a/62150382/1048186) for an answer which answers the OP's question about **local** files. – Josiah Yoder Jan 02 '23 at 20:00
  • Changed accepted answer. In 2022, with the Chromium based edge browser this is no longer a problem. – Warren P Jan 07 '23 at 20:55

7 Answers7

11

This is currently not supported, but the team is evaluating it as an option. For the time being, the easiest way to open a resource in Edge is by using the microsoft-edge: protocol handler. For instance, you could run microsoft-edge:http://stackoverflow.com to open Stack Overflow in Edge.

Sampson
  • 265,109
  • 74
  • 539
  • 565
  • 1
    So this doesn't work, sadly: `start "" "microsoft-edge:file:/c:/path/to/html.txt"` – Warren P Jan 14 '16 at 20:04
  • But if I have a local means to serve HTTP, I could serve the resource myself and then point edge at http://localhost:12345 – Warren P Jan 14 '16 at 20:06
  • 1
    A local server would work; I'm going to reach out to a few team members to see if there is any other way to go about this. Ideally, files would be opened in the default app *selected by the user*. – Sampson Jan 14 '16 at 20:09
  • 8
    that makes sense for a scenario where the user wants the choice. Some users, especially developers, will want to have a specific tool render a specific file, so they can check if that file renders in that browser. Every other browser out there makes this trivially easy. Launch executable, provide url in `file:/c:/path/to/file.html` syntax. Done – Warren P Jan 14 '16 at 20:14
  • 2
    Understood, and agreed. I'm spinning up a thread now with some of our engineers. – Sampson Jan 14 '16 at 20:16
  • 16
    This is absurd! Edge can't open a local file via the command prompt? It seems there must be a way. You can open one by right clicking on a local file and selecting open with Edge... So how does that work? – BuvinJ Jan 19 '16 at 15:24
  • 9
    Just in case it's useful to know, the following used to work for local files via Windows API ShellExecute: `microsoft-edge:file:///c:/path/to/page.htm` - however following a recent Windows 10 Update it no longer works for local files, only for http/https. Up until then I was using this method successfully in a commercial program. I mention it because it could be that the necessary functionality is present already, but maybe needs enabling again by the Edge team. – Cascade Feb 08 '16 at 11:52
  • 6
    I think the Edge team has prioritized general consumer safety to the extreme end of making the Edge browser unusable for developer workflows. – Warren P Feb 08 '16 at 19:03
  • 2
    @Sampson Do you happen to know if the team is still evaluating it as an option? – Mr Lister Jul 02 '19 at 12:32
  • Why evaluate it? JUST DO IT. Edge doesn't support drag and drop either. Very typical of Microsoft somehow. Sadly edge is not going anywhere until some of you Microsoft guys actually start using it and tweaking it. Remember how Bill Gates started a campaign he called "eat your own dog sh*t"? It meant that the developers of MS Office actually had to start using their own product so they could find out what a crash-happy nightmare it was for the public. And it worked! MS Office worked! Then Bill left and we are back to square one. – www-0av-Com May 28 '20 at 19:07
  • 1
    They've fixed it. See my simple solution at bottom of this question. – www-0av-Com Aug 13 '20 at 13:13
  • See [this answer](https://stackoverflow.com/a/62150382/1048186) for an answer which answers the OP's question about **local** files. – Josiah Yoder Jan 02 '23 at 20:00
5

Here is how you can open a PDF for example, with Edge.

Add the following header at the top of your class:

[DllImport("Shell32.dll")]
public static extern int ShellExecuteA(IntPtr hwnd, string lpOperation, string lpFile, string lpParameters, string lpDirecotry, int nShowCmd);

Here is an example of how to make the call.

ShellExecuteA(System.IntPtr.Zero, "open", @"shell:Appsfolder\Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge", "C:\MyFile.pdf", null, 10);

I think this will apply fine to other types of files as well.

Elletlar
  • 3,136
  • 7
  • 32
  • 38
TempGuest
  • 51
  • 1
  • 1
5

New 2020: This only applies to the new, Chromium-based Edge (green-blue logo). It doesn't work with the legacy, "modern" Edge (darkblue logo).

With the new Chromium-based Edge it now works normally, if you include a full absolute path.

Tested at cmd prompt on my Windows 10 64b PC:

"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
file:///C:/MyApplications/MyTestApp.htm

..that's all on one line, simple space between.

I'm no fan of MS, but here's their (slow) Egde Link for the latest version.

Josiah Yoder
  • 3,321
  • 4
  • 40
  • 58
www-0av-Com
  • 707
  • 10
  • 14
  • 1
    @Timo (replied to me on one of earlier answers) says it does not work in powershell, but yes, as you say it does in cmd prompt (or in a batch). Just tested again today after Edge's recent major update, and it is still 100%. I don't know powershell well, but I do know "shelling" out to the operating system is fraught with trips and gotchas from any language - indeed, I'd be incredibly surprised if powershell did not support the equivalent as long as you adapt it. If there is a space in your html path, don't forget to surround it with quotes (no need to convert space to %20 either - tested). – www-0av-Com Aug 13 '20 at 13:10
  • 1
    Similar to this answer, what worked for me in a command prompt was the following on a single line: `start msedge "c:\path\to\html\file.html"` – James Affleck Dec 09 '20 at 16:19
  • Note that this only applies to the new, Chromium-based Edge (green-blue logo). It doesn't work with the legacy, "modern" Edge (darkblue logo). – Julius Bullinger Mar 26 '21 at 10:44
  • From Git Bash, this worked with the filename in `$i`, a PDF in my case: `start msedge $(cygpath -wa $(pwd)/$i);` As the answer and @JamesAffleck's comments suggest, the full absolute path is required. – Josiah Yoder Jan 02 '23 at 19:58
1

This works on my system:

create a share and give yourself access

open in Microsoft Edge, as a simple example: file:////bookmark.html

you can get the hostname via the hostname Powershell command among other ways, you can see all the directories you are sharing by using file explorer, opening "network", at your computer and you should see any shares you have established

not necessarily a deeply satisfying answer but works for what I needed.

1

The following works for local files and also accepts queries (?) and fragments (#) in the URI.

WinAPI / ShellAPI example on a local HTML file:

ShellExecute(
    NULL,
    NULL,
    _T("shell:Appsfolder\\Microsoft.MicrosoftEdge_8wekyb3d8bbwe!MicrosoftEdge"),
    _T("file:///c:/temp/test.html?page=1#anchor-1"),
    NULL,
    SW_SHOWNORMAL);
Martin Schneider
  • 14,263
  • 7
  • 55
  • 58
0

Obtain tool from https://github.com/MicrosoftEdge/edge-launcher

MicrosoftEdgeLauncher file:///C:/Users/me/Documents/homepage.html

Bhuwan
  • 16,525
  • 5
  • 34
  • 57
  • 2
    Unfortunately the question is not asking what to type in the address bar, but how to launch the executable along with the given local page resource. – Blakes Seven Mar 05 '16 at 05:59
  • 1
    It seems you can now run `MicrosoftEdgeLauncher.exe file:///C:/something/index.html` if you go get the tool from github.com/MicrosoftEdge/MicrosoftEdgeLauncher – Warren P Oct 19 '16 at 19:44
0

Microsoft introduced App Aliases, if you check your AppData folder, which is included in Windows path automatically, you will find MicrosoftEdge.exe

 Directory of C:\Users\username\AppData\Local\Microsoft\WindowsApps

06/25/2019  04:13 PM    <DIR>          Backup
10/08/2019  03:35 PM                 0 dbgsrv32.exe
10/08/2019  03:35 PM                 0 dbgsrv64.exe
11/07/2019  01:40 PM    <DIR>          Microsoft.MicrosoftEdge_8wekyb3d8bbwe
10/08/2019  03:35 PM    <DIR>          Microsoft.WinDbg_8wekyb3d8bbwe
11/07/2019  01:40 PM                 0 MicrosoftEdge.exe
10/08/2019  03:35 PM                 0 WinDbgX.exe
               4 File(s)              0 bytes
               3 Dir(s)  119,020,060,672 bytes free

Unfortunately the alias does not appear to open HTML files or response to any CLI, unlike the working WinDbgX.

So once Microsoft implements shell CLI for Edge, that will be the correct invocation method.

One workaround, is to type in the URL bar, a file:// URI like the following (note: / is needed):

file:///D:/random/path/file.html
Kevin
  • 2,761
  • 1
  • 27
  • 31