SomeRandomGuy
SomeRandomGuy

Reputation: 11

Check if Microsoft Edge is already opened

my problem is following:

I am currently opening up Microsoft Edge through .NET code to be able to read PFD-Files. But I only want to open Edge with a PDF-File, if it's not already opened.

The problem with Edge is, that the window is hosted by the ApplicationFrameHost, which also hosts other Windows Apps like Minesweaper from the store. So when I am opening e.g. Minesweaper after opening my pdf file with Edge, the current ApplicationFrameHost MainWindowTitle is "Minesweaper".

But if I start my code, it should check at the beginning if Edge is already opened, but I can't check it through the ApplicationFrameHost MainWindowTitle, because it is from the current active ApplicationFrameHost, which is "Minesweaper", because I it was the last active ApplicationFrameHost window.

I also can't check if the MicrosoftEdgeCP process is running, because it is always running, even if I close Microsoft Edge.

Do you have any solutions for my problem?

Upvotes: 1

Views: 3132

Answers (1)

Zhi Lv
Zhi Lv

Reputation: 21646

When you launch Edge (at least) two process get created: MicrosoftEdge and MicrosoftEdgeCP.

You can try to use the following code to check whether the Edge browser is opened:

//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();

If we could get the ProcessName, means the Edge is already opened. The above code, works well on my side.

Upvotes: 0

Related Questions