AbstractDissonance
AbstractDissonance

Reputation: 1

C# - Get all open browsing tabs in all instances of firefox

How can I get URLs of open pages from Chrome and Firefox?

How can I get URLs of open pages from Chrome and Firefox?

http://hintdesk.com/c-automationelement-left-click-and-send-keys/

How can I check if website is already opened in a webbrowser?

etc...

all fail. Either they return only the top most opened tab or do not work at all.

I need to check and see if firefox is opened at all to a url, if it is then refresh and focus that tab/url, if not open firefox to that page.

This is not a firefox addon or anything really having to do with firefox. That is my browser of choice so that is the one I desire to use. All this is to avoid consecutively opening the same url each time my app is run.

Upvotes: 4

Views: 3023

Answers (1)

Jayesh Tank
Jayesh Tank

Reputation: 129

You can print window name using System.Runtime.InteropServices.

[DllImport("user32.dll")]
static extern int GetWindowTextLength(IntPtr hWnd);

[DllImport("user32.dll")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

public static void PrintBrowserTabName()
{
    var browsersList = new List<string>
    {
        "chrome",
        "firefox",
        "iexplore",
        "safari",
        "opera",
        "edge"
    };

    foreach (var singleBrowser in browsersList)
    {
        var process = Process.GetProcessesByName(singleBrowser);
        if (process.Length > 0)
        {
            foreach (Process singleProcess in process)
            {
                IntPtr hWnd = singleProcess.MainWindowHandle;
                int length = GetWindowTextLength(hWnd);

                StringBuilder text = new StringBuilder(length + 1);
                GetWindowText(hWnd, text, text.Capacity);
                Console.WriteLine(text.ToString());
            }
        }
    }
}

Upvotes: 2

Related Questions