using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Automation; using System.Diagnostics; using System.Threading; using System.Windows.Forms; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { try { Console.WriteLine("starting notepad"); Process p = Process.Start("notepad.exe"); Console.WriteLine("getting desktop"); AutomationElement aeDesktop = AutomationElement.RootElement; Console.WriteLine("waiting for notepad"); Thread.Sleep(5000); AutomationElement aeForm = AutomationElement.FromHandle(p.MainWindowHandle); Console.WriteLine("found notepad: " + aeForm.GetCurrentPropertyValue(AutomationElement.NameProperty)); Console.WriteLine("searching main text field"); PropertyCondition cond = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document); AutomationElement textProvider = aeForm.FindFirst(TreeScope.Descendants, cond); Console.WriteLine("staring to write time"); TextPattern textpatternPattern = textProvider.GetCurrentPattern(TextPattern.Pattern) as TextPattern; for (int i = 0; i < 1000; i++) { textProvider.SetFocus(); SendKeys.SendWait("number: " + i + " time: " + DateTime.Now + "\n"); Thread.Sleep(5000); } Console.WriteLine("text in main text field"); Console.WriteLine(textpatternPattern.DocumentRange.GetText(-1)); } catch(Exception e) { Console.WriteLine(e.Message); } Thread.Sleep(5000); } } }
number: 0 time: 28.10.2010 09:23:39 number: 1 time: 28.10.2010 09:23:45 number: 2 time: 28.10.2010 09:23:52 number: 3 time: 28.10.2010 09:23:58 number: 4 time: 28.10.2010 09:24:04
number: 0 time: 28.10.2010 09:23:39 number: 1 time: 28.10.2010 09:23:45 number: 2 time: 28.10.2010 09:23:52 number: 3 time: 28.10.2010 09:23:58 number: 4 time: 28.10.2010 09:24:04 number: 8 time: 28.10.2010 09:24:39 number: 9 time: 28.10.2010 09:24:46 number: 10 time: 28.10.2010 09:24:53 number: 11 time: 28.10.2010 09:25:01 number: 12 time: 28.10.2010 09:25:08