Get EDGE's Urls in VB.NET Project

Y H 60 Reputation points
2023-03-15T05:44:33.04+00:00

Hi all,

I'm using VB.NET to launch an Edge browser with a specific URL in an app mode. In this, I need to include a code to check whether the same URL is already open in any instance of Edge browser. Is there any way to get the list of URLs that are currently opened in the Edge browser in VB.NET file?

Microsoft Edge
Microsoft Edge
A Microsoft cross-platform web browser that provides privacy, learning, and accessibility tools.
1,452 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
1,971 questions
No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 11,801 Reputation points Microsoft Vendor
    2023-03-16T01:37:16.53+00:00

    Hi,welcome to MicrosoftQ&A. You can refer to the following code that I have tested.

    The following code is referenced from Castorix31's C# code in the following question. https://learn.microsoft.com/en-us/answers/questions/1189369/how-can-i-get-the-edge-applications-information

    Imports Interop.UIAutomationClient
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim hWndEdge As IntPtr = IntPtr.Zero
            Dim procEdge As System.Diagnostics.Process() = System.Diagnostics.Process.GetProcessesByName("msedge")
    
            If procEdge.Length <= 0 Then
                Console.WriteLine("Microsoft Edge is not running")
            Else
    
                For Each proc As System.Diagnostics.Process In procEdge
    
                    If proc.MainWindowHandle = IntPtr.Zero Then
                        Continue For
                    Else
                        hWndEdge = proc.MainWindowHandle
                        Exit For
                    End If
                Next
            End If
    
            If hWndEdge <> IntPtr.Zero Then
                Dim pUIAutomation As IUIAutomation = New CUIAutomation()
                Dim windowElement As IUIAutomationElement = pUIAutomation.ElementFromHandle(hWndEdge)
    
                If windowElement IsNot Nothing Then
                    Dim elementArray As IUIAutomationElementArray = Nothing
                    Dim condition As IUIAutomationCondition = Nothing
                    condition = pUIAutomation.CreateTrueCondition()
                    elementArray = windowElement.FindAll(TreeScope.TreeScope_Descendants, condition)
                    Dim elementAddressBar As IUIAutomationElement = Nothing
    
                    If elementArray IsNot Nothing Then
                        Dim nNbItems As Integer = elementArray.Length
    
                        For nItem As Integer = 0 To nNbItems - 1
                            Dim element As IUIAutomationElement = elementArray.GetElement(nItem)
                            Dim sName As String = element.CurrentName
                            Dim sAutomationId As String = element.CurrentAutomationId
                            Console.WriteLine("Name : {0} - AutomationId : {1}", sName, sAutomationId)
                            Dim element5 As IUIAutomationElement5 = CType(element, IUIAutomationElement5)
                            Dim nLandmakType As Integer = element5.CurrentLandmarkType
    
                            If nLandmakType = 80004 Then
                                elementAddressBar = element
                                Exit For
                            End If
                        Next
    
                        If elementAddressBar IsNot Nothing Then
                            Dim pPattern As Object = Nothing
                            pPattern = elementAddressBar.GetCurrentPattern(10018)
    
                            If pPattern IsNot Nothing Then
                                Dim pLegacyIAccessiblePattern As IUIAutomationLegacyIAccessiblePattern = CType(pPattern, IUIAutomationLegacyIAccessiblePattern)
                                Dim sValue As String = pLegacyIAccessiblePattern.CurrentValue
                                MessageBox.Show(String.Format("Current URL = {0}", sValue), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
                            End If
                        End If
                    End If
                End If
            End If
        End Sub
    End Class
    

    Best Regards.
    Jiachen Li
    ----------
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful