static void Main(string[] args) { try { var process = Process.Start(@"C:\Windows\system32\control.exe", @"/name Microsoft.DefaultPrograms /page pageDefaultProgram\pageAdvancedSettings?pszAppName=google%20chrome"); var exited = process.WaitForExit(3000); if (!exited) { process.Kill(); throw new Exception("Control panel process didn't exit"); } var uiAutomation = new CUIAutomation(); var window = FindAndWait(uiAutomation.GetRootElement(), TreeScope.TreeScope_Children, uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Set Program Associations"), TimeSpan.FromSeconds(3)); try { //Sadly, the window can't be hidden or moved to another desktop since doing so results in UI Automation not seeing it. var listView = FindAndWait(window, TreeScope.TreeScope_Descendants, uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Program Association List"), TimeSpan.FromSeconds(3)); var save = FindAndWait(window, TreeScope.TreeScope_Descendants, uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, "Save"), TimeSpan.FromSeconds(3)); var listItems = new[] { ".htm", ".html", "HTTP", "HTTPS", }.Select(name => FindAndWait( listView, TreeScope.TreeScope_Descendants, uiAutomation.CreateAndCondition( uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_NamePropertyId, name), uiAutomation.CreatePropertyCondition(UIA_PropertyIds.UIA_ControlTypePropertyId, UIA_ControlTypeIds.UIA_ListItemControlTypeId) ), TimeSpan.FromSeconds(3) )).ToArray(); foreach (var listItem in listItems) { var toggle = ((IUIAutomationTogglePattern)listItem.GetCurrentPattern(UIA_PatternIds.UIA_TogglePatternId)); while (toggle.CurrentToggleState != ToggleState.ToggleState_On) //TODO: Watch out for infinite looping here! { toggle.Toggle(); } } ((IUIAutomationInvokePattern)save.GetCurrentPattern(UIA_PatternIds.UIA_InvokePatternId)).Invoke(); } finally { ((IUIAutomationWindowPattern)window.GetCurrentPattern(UIA_PatternIds.UIA_WindowPatternId)).Close(); } } catch //Ensures all nested finally blocks execute { throw; } }
public void Test1() { IUIAutomation a = new CUIAutomation(); IUIAutomationElement x = a.GetRootElement(); IUIAutomation2 b = new CUIAutomation8(); }
static void Main(string[] args) { // Instantiate the UIA object: IUIAutomation _automation = new CUIAutomation(); // Get the root element IUIAutomationElement rootElement = _automation.GetRootElement(); // Get its name string rootName = rootElement.CurrentName; Console.WriteLine( "The root automation element's name should be 'Desktop'."); Console.WriteLine("The actual value is: '{0}'", rootName); }
static void Main(string[] args) { // Instantiate the UIA object: IUIAutomation _automation = new CUIAutomation(); // Get the root element IUIAutomationElement rootElement = _automation.GetRootElement(); // Get its name string rootName = rootElement.CurrentName; Console.WriteLine( "The root automation element's name should be 'Desktop'."); Console.WriteLine("The actual value is: '{0}'", rootName); }
/// <summary> /// Get DesktopElement based on Process Id. /// </summary> /// <param name="pid"></param> /// <returns>return null if fail to get an element by process Id</returns> public static DesktopElement ElementFromProcessId(int pid) { IUIAutomationElement root = null; DesktopElement element = null; IUIAutomationCondition condition = null; try { // check whether process exist first. // if not, it will throw an ArgumentException using (var proc = Process.GetProcessById(pid)) { root = UIAutomation.GetRootElement(); condition = UIAutomation.CreatePropertyCondition(PropertyType.UIA_ProcessIdPropertyId, pid); var uia = root.FindFirst(TreeScope.TreeScope_Descendants, condition); element = ElementFromUIAElement(uia); } } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception ex) { // report and let it return null ex.ReportException(); } #pragma warning restore CA1031 // Do not catch general exception types finally { if (root != null) { Marshal.ReleaseComObject(root); } if (condition != null) { Marshal.ReleaseComObject(condition); } } return(element); }
/// <summary> /// Get DesktopElement based on Process Id. /// </summary> /// <param name="pid"></param> /// <returns>return null if fail to get an element by process Id</returns> public static DesktopElement ElementFromProcessId(int pid) { IUIAutomationElement root = null; DesktopElement element = null; IUIAutomationCondition condition = null; try { // check whether process exist first. // if not, it will throw an ArgumentException using (var proc = Process.GetProcessById(pid)) { root = UIAutomation.GetRootElement(); condition = UIAutomation.CreatePropertyCondition(PropertyType.UIA_ProcessIdPropertyId, pid); var uia = root.FindFirst(TreeScope.TreeScope_Descendants, condition); element = ElementFromUIAElement(uia); } } catch (Exception ex) { //silent and let it return null Debug.WriteLine(ex); } finally { if (root != null) { Marshal.ReleaseComObject(root); } if (condition != null) { Marshal.ReleaseComObject(condition); } } return(element); }
public void ProcessHandlerDiagsTest() { var automation = new CUIAutomation(); var root = automation.GetRootElement(); var elements = root.FindAll(TreeScope.TreeScope_Children, automation.CreateTrueCondition()); for (var i = 0; i < elements.Length; i++) { var element = elements.GetElement(i); Debug.Print(element.CurrentClassName + ";" + element.CurrentName + ";" + element.CurrentProcessId + ";" + element.CurrentFrameworkId + ";" + element.CurrentNativeWindowHandle); } Debug.Print("---"); var frameElements = root.FindAll(TreeScope.TreeScope_Children, automation.CreatePropertyConditionEx( UIA_PropertyIds.UIA_ClassNamePropertyId, "ApplicationFrameWindow", PropertyConditionFlags.PropertyConditionFlags_IgnoreCase)); if (frameElements.Length == 0) { Debug.Print("No UWP apps"); return; } for (var i = 0; i < frameElements.Length; i++) { var frameElement = frameElements.GetElement(i); var subElements = frameElement.FindAll(TreeScope.TreeScope_Children, automation.CreateTrueCondition()); for (var j = 0; j < subElements.Length; j++) { var subElement = subElements.GetElement(j); Debug.Print(subElement.CurrentClassName + ";" + subElement.CurrentName + ";" + subElement.CurrentProcessId + ";" + subElement.CurrentFrameworkId + ";" + subElement.CurrentNativeWindowHandle); } Debug.Print("--"); } }
public void Start() { _automation = new CUIAutomation(); EnumarateChildren(_automation.GetRootElement()); }