【问题标题】:Check if Microsoft Edge is already opened检查 Microsoft Edge 是否已打开
【发布时间】:2019-03-05 10:10:16
【问题描述】:

我的问题如下:

我目前正在通过 .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。

你有解决我的问题的办法吗?

【问题讨论】:

    标签: .net microsoft-edge mainwindow


    【解决方案1】:

    当您启动 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 已经打开。上面的代码对我来说效果很好。

    【讨论】:

    • 用这个检查的问题是,即使你关闭了 Microsoft Edge,这两个进程也会继续运行。我用刚打开的电脑检查了它。没有这样的过程。但是当我启动 Edge 并再次关闭它时,这些进程仍在后台。因此,这将在当天第一次打开 Edge 时起作用,但如果它已经打开,则不会。
    • 您的 Windows 操作系统环境是什么?我在我的 PC 上使用 Windows 10 Enterprise OS,如果我关闭 Edge,这些进程将被关闭。因此,您的 Edge 浏览器似乎没有完全关闭。这里是a thread about edge browser seems to be not completely closing,可以参考。另外,您可以尝试reset the Edge browser
    • 我会在周末看看它并告诉你我的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 2019-04-29
    • 2012-08-30
    • 1970-01-01
    相关资源
    最近更新 更多