我的问题如下:
我目前正在通过.NET代码开放Microsoft Edge以便能够读取PFD文件。但我只想用PDF文件打开Edge,如果尚未打开的话。
Edge的问题在于,该窗口由ApplicationFrameHost托管,后者还从商店托管其他Windows应用程序,如Minesweaper。所以当我打开时,例如用Edge打开我的pdf文件后,Minesweaper,当前的ApplicationFrameHost MainWindowTitle是“Minesweaper”。
但是如果我启动我的代码,它应该在开始时检查Edge是否已经打开,但我无法通过ApplicationFrameHost MainWindowTitle检查它,因为它来自当前活动的ApplicationFrameHost,即“Minesweaper”,因为我是最后一个活动的ApplicationFrameHost窗口。
我也无法检查MicrosoftEdgeCP进程是否正在运行,因为它始终在运行,即使我关闭了Microsoft Edge。
你对我的问题有什么解决方案吗?
当您启动Edge(至少)创建两个进程时:MicrosoftEdge和MicrosoftEdgeCP。
您可以尝试使用以下代码检查是否已打开Edge浏览器:
//We need to find the most recent MicrosoftEdgeCP process that is active
Process[] EdgeCPProcessList = Process.GetProcessesByName("MicrosoftEdgeCP");
Process newestEdgeCPProcess = null;
foreach (Process theprocess in EdgeCPProcessList)
{
if (newestEdgeCPProcess == null || theprocess.StartTime > newestEdgeCPProcess.StartTime)
{
newestEdgeCPProcess = theprocess;
Console.WriteLine("EdgeCP Process: "+theprocess.ProcessName.ToString());
}
}
Process[] edgeProcessList = Process.GetProcessesByName("MicrosoftEdge");
Process newestEdgeProcess = null;
foreach (Process edgeProcess in edgeProcessList)
{
if (newestEdgeProcess == null || edgeProcess.StartTime > newestEdgeProcess.StartTime)
{
newestEdgeProcess = edgeProcess;
Console.WriteLine("Edge Process: " + edgeProcess.ProcessName.ToString());
}
}
Console.ReadKey();
如果我们可以获得ProcessName,则意味着Edge已经打开。上面的代码在我这边很好用。