Get Current urls from Microsoft edge using c# windows form application

harsha111111 121 Reputation points
2021-07-09T13:59:43.807+00:00

I tried

                          using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Automation;
using System.Windows.Forms;

namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    private void Form1_Load(object sender, EventArgs e)
    {
        timer1.Start();
    }
    public static AutomationElement GetEdgeCommandsWindow(AutomationElement edgeWindow)
    {
        return edgeWindow.FindFirst(TreeScope.Children, new AndCondition(
            new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window),
            new PropertyCondition(AutomationElement.NameProperty, "Microsoft Edge")));
    }

    public static string GetEdgeUrl(AutomationElement edgeCommandsWindow)
    {
        var adressEditBox = edgeCommandsWindow.FindFirst(TreeScope.Children,
            new PropertyCondition(AutomationElement.AutomationIdProperty, "addressEditBox"));

        return ((TextPattern)adressEditBox.GetCurrentPattern(TextPattern.Pattern)).DocumentRange.GetText(int.MaxValue);
    }

    public static string GetEdgeTitle(AutomationElement edgeWindow)
    {
        var adressEditBox = edgeWindow.FindFirst(TreeScope.Children,
            new PropertyCondition(AutomationElement.AutomationIdProperty, "TitleBar"));

        return adressEditBox.Current.Name;
    }

    [DllImport("user32")]
    public static extern IntPtr GetDesktopWindow();
    public enum TreeScope { }
    private void timer1_Tick(object sender, EventArgs e)
    {
        AutomationElement main = AutomationElement.FromHandle(GetDesktopWindow());
        foreach (AutomationElement child in main.FindAll(TreeScope.Children, Condition.TrueCondition))
        {
            AutomationElement window = GetEdgeCommandsWindow(child);
            if (window == null) // not edge
                continue;



        }

    }
}

}

But AutomationElement main = AutomationElement.FromHandle(GetDesktopWindow());
foreach (AutomationElement child in main.FindAll(TreeScope.Children, Condition.TrueCondition))
shows error and message is THE TYPE 'TREESCOPE' IS DEFINED IN AN ASSEMBLY THAT IS NOT REFERED. YOU MUST ADD A REFERENCE TO ASSEMBLY 'UIAUTOMATIONTYPES VERSION:4.0.0.0 CULTURE = NEUTRAL'

If anyone knows how to solve this issue please do comment

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,169 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 47,461 Reputation points
    2021-07-09T14:22:22.077+00:00

    You have defined TreeScope as an empty enum in your code. This comes from the UI Automation via COM, not your code.

    You're also relying on System.Windows.Automation which is using the old UIA libraries and is known to have lots of issues. Most people recommend either using COM directly (so you can use the latest UIA objects) or another third party library.

    1. Remove your definition of TreeScope.
    2. Right click the "unresolved identifier" error in IDE and use the quick action to resolve to using System.Windows.Automation from UIAutomationTypes

    This will bring in the assembly that contains the corresponding UIA COM references that you need to go with the System.Windows.Automation namespace.

    After that you'll likely get a MDA error about COM interfaces, depending upon the framework you're targeting. In my experience it is easier to find the top level window using standard .NET code like calling Process.GetProcesses. It is faster and easier. Once you have the "Edge" windows you can then use UIA.

    0 comments No comments