如何从C#中打开“ Microsoft Edge”并等待其关闭?

Avi*_*ger 4 .net c# process windows-10 microsoft-edge

我正在构建Windows窗体应用程序,我想通过具有特定URL的应用程序打开“ Microsoft Edge”,然后等待用户关闭Edge窗口。

我尝试使用以下代码:

using (Process p = Process.Start("microsoft-edge:www.mysite.com"))
{
    p.WaitForExit();
}
Run Code Online (Sandbox Code Playgroud)

当我执行此代码时,Edge正在使用正确的URL启动...但是得到了空对象引用。我从中获得的“ p”对象Process.Start为空。

我认为这与Windows应用程序的重用有关。

有没有人有解决方法/知道如何才能等待用户关闭Edge?

Avi*_*ger 5

最终,我确实做到了:启动Edge(至少)时,创建了两个过程:MicrosoftEdge和MicrosoftEdgeCP。

MicrosoftEdgeCP-foreach选项卡。因此,我们可以“等待”刚刚创建的新选项卡过程。

//Edge process is "recycled", therefore no new process is returned.
Process.Start("microsoft-edge:www.mysite.com");

//We need to find the most recent MicrosoftEdgeCP process that is active
Process[] edgeProcessList = Process.GetProcessesByName("MicrosoftEdgeCP");
Process newestEdgeProcess = null;

foreach (Process theprocess in edgeProcessList)
{
    if (newestEdgeProcess == null || theprocess.StartTime > newestEdgeProcess.StartTime)
    {
        newestEdgeProcess = theprocess;
    }
}

newestEdgeProcess.WaitForExit();
Run Code Online (Sandbox Code Playgroud)