Aakash Singh
Aakash Singh

Reputation: 1062

Getting the active url of browser in C# windows form application

I have created a window form application. This App get the active url of browser and save this into the text file. And this works fine in chrome & IE.

But when i use firefox, this will not work. This code fails to get the active url of firefox browser.

I don't know why this happening.

I am using the following code to find the URL

 public string GetBrowsedUrl()
        {

            IntPtr hwnd = APIFuncs.getforegroundWindow();
            Int32 pid = APIFuncs.GetWindowProcessID(hwnd);
            Process process = Process.GetProcessById(pid);
            string appId = proc.Id.ToString();
            string appName = proc.ProcessName;
            string appltitle = APIFuncs.ActiveApplTitle().Trim().Replace("\0", "");
            if (process == null)
                throw new ArgumentNullException("process");

            if (process.MainWindowHandle == IntPtr.Zero)
                return null;

            AutomationElement element = AutomationElement.FromHandle(process.MainWindowHandle);
            if (element == null)
                return null;

            AutomationElement edit = element.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));


            string result = ((ValuePattern)edit.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
            return result;
        }

Upvotes: 0

Views: 1929

Answers (2)

Aakash Singh
Aakash Singh

Reputation: 1062

Finally i found the answer

public string GetBrowsedUrl(Process process)
{
    if (process.ProcessName == "firefox")
    {
        if (process == null)
            throw new ArgumentNullException("process");

        if (process.MainWindowHandle == IntPtr.Zero)
            return null;

        AutomationElement element = AutomationElement.FromHandle(process.MainWindowHandle);
        if (element == null)
            return null;

        AutomationElement doc = element.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document));
        if (doc == null)
            return null;

        return ((ValuePattern)doc.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
    }
    else
    {
        if (process == null)
            throw new ArgumentNullException("process");

        if (process.MainWindowHandle == IntPtr.Zero)
            return null;

        AutomationElement element = AutomationElement.FromHandle(process.MainWindowHandle);
        if (element == null)
            return null;

        AutomationElement edit = element.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));


        string result = ((ValuePattern)edit.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
        return result;
    }

}

Upvotes: 3

habib
habib

Reputation: 2446

You can not use this code for firefox too.

I recommend a third party library named NDde to do this very easily. Here is NDde link

    public string GetFirefoxUrl()
    {
        try
        {
            Process[] pname = Process.GetProcessesByName("Firefox");
            if (pname.Length != 0)
            {
                DdeClient dde = new DdeClient("Firefox", "WWW_GetWindowInfo");
                dde.Connect();
                string url = dde.Request("URL", int.MaxValue);
                url= url.Replace("\"", "").Replace("\0", "");
                dde.Disconnect();
                return url;
            }
            else
                return null;
        }
        catch
        {

            return null;
        }
    }

Upvotes: 0

Related Questions