Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

My application find the currently open URL from chrome when user press "enter" key.
But actually when I press the "enter" key sometime application is crashed and all time it doesn't give me a opened URL.

And most stressful problem is application works good if I set debug point before or at this
C#
AutomationElement elm = AutomationElement.FromHandle(new IntPtr(handle));

line.

If trying without debug point its not working.

And yes main thing is that there is no any exception.

here I attach the full code of my method which return the URL of chrome opened tab.
C#
private string GetCurrentChromeUrl()
{
        try
        {
            string url = null;
            int handle = GetForegroundWindow();
            foreach (Process P in Process.GetProcessesByName("chrome"))
            {
                if(P.MainWindowHandle.ToInt32() > 0)
                {
                    handle = P.MainWindowHandle.ToInt32();
                    break;
                 }
             }
             AutomationElement elm = AutomationElement.FromHandle(new 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 very upset by facing this error since two days.

Please help me to solve this problem.
Posted
Updated 9-Sep-14 21:37pm
v2

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