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.
This test works for me (code tested in a click on a Button...) =>
IntPtr hWndEdge = IntPtr.Zero;
System.Diagnostics.Process[] procEdge = System.Diagnostics.Process.GetProcessesByName("msedge");
if (procEdge.Length <= 0)
Console.WriteLine("Microsoft Edge is not running");
else
{
foreach (System.Diagnostics.Process proc in procEdge)
{
if (proc.MainWindowHandle == IntPtr.Zero)
continue;
else
{
hWndEdge = proc.MainWindowHandle;
break;
}
}
}
if (hWndEdge != IntPtr.Zero)
{
IUIAutomation pUIAutomation = new CUIAutomation();
IUIAutomationElement windowElement = pUIAutomation.ElementFromHandle(hWndEdge);
if (windowElement != null)
{
IUIAutomationElementArray elementArray = null;
IUIAutomationCondition condition = null;
condition = pUIAutomation.CreateTrueCondition();
elementArray = windowElement.FindAll(TreeScope.TreeScope_Descendants, condition);
IUIAutomationElement elementAddressBar = null;
if (elementArray != null)
{
int nNbItems = elementArray.Length;
for (int nItem = 0; nItem <= nNbItems - 1; nItem++)
{
IUIAutomationElement element = elementArray.GetElement(nItem);
string sName = element.CurrentName;
string sAutomationId = element.CurrentAutomationId;
Console.WriteLine("Name : {0} - AutomationId : {1}", sName, sAutomationId);
IUIAutomationElement5 element5 = (IUIAutomationElement5)element;
int nLandmakType = element5.CurrentLandmarkType;
if (nLandmakType == 80004) // UIA_LandmarkTypeIds.UIA_SearchLandmarkTypeId
{
elementAddressBar = element;
break;
}
}
if (elementAddressBar != null)
{
Object pPattern = null;
pPattern = elementAddressBar.GetCurrentPattern(10018); // UIA_PatternIds.UIA_LegacyIAccessiblePatternId
if (pPattern != null)
{
IUIAutomationLegacyIAccessiblePattern pLegacyIAccessiblePattern = (IUIAutomationLegacyIAccessiblePattern)pPattern;
String sValue = pLegacyIAccessiblePattern.CurrentValue;
MessageBox.Show(String.Format("Current URL = {0}", sValue), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
}