问题
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?
回答1:
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.
回答2:
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.
回答3:
Obtain tool from https://github.com/MicrosoftEdge/edge-launcher
MicrosoftEdgeLauncher file:///C:/Users/me/Documents/homepage.html
回答4:
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.
回答5:
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);
回答6:
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
来源:https://stackoverflow.com/questions/34798285/how-can-i-open-a-local-html-file-in-microsoft-edge-browser