如何从 C# 打开 "Microsoft Edge" 并等待它关闭?
How to open "Microsoft Edge" from c# and wait for it to be closed?
我正在构建一个 Windows 表单应用程序,我想通过我的应用程序使用特定 URL 打开 "Microsoft Edge" 并等待用户关闭 Edge Window.
我用这段代码试过了:
using (Process p = Process.Start("microsoft-edge:www.mysite.com"))
{
p.WaitForExit();
}
当我执行此代码时,Edge 以正确的 URL 启动...但得到一个空对象引用。我从 Process.Start
得到的 "p" 对象是空的。
我认为这与 Windows 应用程序的重用有关。
有没有人workaround/have知道如何等待用户关闭 Edge?
从旧的解决方案 - 使用 WebBrowser 控件加载 HTML。取回数据后,使用 ObjectForScripting 调用 c# 方法以在完成时发出通知。
见http://www.codeproject.com/Tips/130267/Call-a-C-Method-From-JavaScript-Hosted-in-a-WebBrowser
最后我确实做到了:
当您启动 Edge(至少)时,会创建两个进程:
MicrosoftEdge 和 MicrosoftEdgeCP。
MicrosoftEdgeCP - 每个选项卡。因此,我们可以 "wait" 在刚刚创建的这个新选项卡进程上。
//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();
我正在构建一个 Windows 表单应用程序,我想通过我的应用程序使用特定 URL 打开 "Microsoft Edge" 并等待用户关闭 Edge Window.
我用这段代码试过了:
using (Process p = Process.Start("microsoft-edge:www.mysite.com"))
{
p.WaitForExit();
}
当我执行此代码时,Edge 以正确的 URL 启动...但得到一个空对象引用。我从 Process.Start
得到的 "p" 对象是空的。
我认为这与 Windows 应用程序的重用有关。
有没有人workaround/have知道如何等待用户关闭 Edge?
从旧的解决方案 - 使用 WebBrowser 控件加载 HTML。取回数据后,使用 ObjectForScripting 调用 c# 方法以在完成时发出通知。
见http://www.codeproject.com/Tips/130267/Call-a-C-Method-From-JavaScript-Hosted-in-a-WebBrowser
最后我确实做到了: 当您启动 Edge(至少)时,会创建两个进程: MicrosoftEdge 和 MicrosoftEdgeCP。
MicrosoftEdgeCP - 每个选项卡。因此,我们可以 "wait" 在刚刚创建的这个新选项卡进程上。
//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();