Get Firefox URL?

15,623

Solution 1

Building on Rob Kennedy's answer and using NDde

using NDde.Client;

class Test
{
        public static string GetFirefoxURL()
        {
            DdeClient dde = new DdeClient("Firefox", "WWW_GetWindowInfo");
            dde.Connect();
            string url = dde.Request("URL", int.MaxValue);
            dde.Disconnect();
            return url;
        }
}

NB: This is very slow. It takes a few seconds on my computer. The result will look something like this :

"http://stackoverflow.com/questions/430614/get-firefox-url","Get Firefox URL? - Stack Overflow",""

More info on browser DDE here.

Solution 2

For most browsers, including Internet Explorer, Navigator, Firefox, and Opera, the supported and sanctioned way of doing this is to use DDE. The topic name in all of them is WWW_GetWindowInfo; only the name of the target window varies. That technique will be difficult for you, though, because .Net doesn't support DDE. If you can find a way to get around that limitation, you'll be all set.

Solution 3

it seems that this might be difficult, here's some discussion on it: http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/c60b1699-9fd7-408d-a395-110c1cd4f297/

Solution 4

You may want to check into the source code of WatiN. Their next version is open source and supports firefox, so I would imagine the functionality for doing this is in it.

Solution 5

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindowEx(IntPtr parentHandle,
    IntPtr childAfter, string className, IntPtr windowTitle);

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern int SendMessage(IntPtr hWnd,
        int msg, int wParam, StringBuilder ClassName);

    private static string GetURL(IntPtr intPtr, string programName, out string url)
    {
        string temp=null;
        if (programName.Equals("chrome"))
        {
            var hAddressBox = FindWindowEx(intPtr, IntPtr.Zero, "Chrome_OmniboxView", IntPtr.Zero);
            var sb = new StringBuilder(256);
            SendMessage(hAddressBox, 0x000D, (IntPtr)256, sb);
            temp = sb.ToString();
        } 
        if (programName.Equals("iexplore"))
        {
            foreach (InternetExplorer ie in new ShellWindows())
            {
                var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(ie.FullName);
                if (fileNameWithoutExtension != null)
                {
                    var filename = fileNameWithoutExtension.ToLower();
                    if (filename.Equals("iexplore"))
                    {
                        temp+=ie.LocationURL + " ";
                    }
                }
            }
        }
        if (programName.Equals("firefox"))
       {
            DdeClient dde = new DdeClient("Firefox", "WWW_GetWindowInfo");
            dde.Connect();
            string url1 = dde.Request("URL", int.MaxValue);
            dde.Disconnect();
            temp = url1.Replace("\"","").Replace("\0","");
        }
        url = temp;
        return temp;
    }

Please do following to run this code Add Reference > Com > Microsoft.Internet.Controls from VS.NET in your project

Download the bin from http://ndde.codeplex.com/ for DdeClient class and add it to your project

Please Let me know if any issue

Share:
15,623
Leon Tayson
Author by

Leon Tayson

.NET Developer

Updated on June 04, 2022

Comments

  • Leon Tayson
    Leon Tayson almost 2 years

    How can I get the url from a running instance of firefox using .NET 2.0 windows/console app? C# or VB codes will do.

    Thanks!

  • Leon Tayson
    Leon Tayson over 15 years
    I've edited the question to add more details. I'm doing a winforms application and i need to get the browser URL. I already have the codes for the IE Url. Thanks!
  • PhiLho
    PhiLho almost 15 years
    No. Because I don't know C#. I would do that with AutoHotkey... :-P
  • Ayorus
    Ayorus over 6 years
    I was using this piece of code since a couple of year ago. However with the release of a new version of FF it is not working anymore. Do you know if there any other way to achieve the same result?