Click here to Skip to main content
15,907,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private string GetCurrentChromeUrl()
    {
        try
        {
            string url = null;

            int handle = GetForegroundWindow();
            AutomationElement elm = AutomationElement.FromHandle((IntPtr)handle);
            AutomationElement elmUrlBar = elm.FindFirst(TreeScope.Descendants,
                  new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));
            if (elmUrlBar != null)
            {
                AutomationPattern[] patterns = elmUrlBar.GetSupportedPatterns();
                if (patterns.Length > 0)
                {
                    ValuePattern val = (ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0]);
                    url = val.Current.Value;
                    //break;
                }
            }
            return url;
        }
        catch (Exception e1)
        {
            return "";
        }
   }

I'm trying to find the URL from google chrome browser.

And I'm using above code It is work good in other application but in my case it stops my application.

But the main issue is that it will work fine when I'm debugging it, so what wrong when no debugging.

It work fine if I point debug point before

C#
AutomationElement elm = AutomationElement.FromHandle((IntPtr)handle);


But when after this line debugger doesn't work due to system hang

please give your solutions.

thanx in advance
Posted
Comments
kbrandwijk 6-Sep-14 7:16am    
Can you also post the GetForegroundWindow method?
BharatPrajapati212 6-Sep-14 7:31am    
[DllImport("user32.dll")]
static extern int GetForegroundWindow();
BharatPrajapati212 8-Sep-14 2:38am    
Sir give me some solution
BharatPrajapati212 6-Sep-14 7:34am    
It is return handle value of currently running application.

all above given code works perfectly, if I debugging it. but not working without debug point.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900