This project is read-only.

Opening a File with Notepad

Jun 5, 2011 at 6:54 PM

Hi Guys

have been playing with White to automate opening a file with Notepad

code snippet below:


            Window openWindow = (Window)mainWindow.Get(SearchCriteria.ByControlType(typeof(Window)).AndByText("Open"));
          if (openWindow.IsModal)
                Console.WriteLine("Yes it is");//returns tru
            var searchCriteria = SearchCriteria.ByAutomationId("1148");
            TextBox fileName = (TextBox)openWindow.Get<TextBox>(searchCriteria);

            fileName.Text = @"C:\workiing\context.txt";

            searchCriteria = SearchCriteria.ByText("Open");
            Button nextButton = (Button)openWindow.Get<Button>(searchCriteria);

It doesnt seem to find the open window.

Any ideas??


Jun 6, 2011 at 7:45 AM


try to get the window from Application object.



Jun 6, 2011 at 10:58 PM
Edited Jun 6, 2011 at 10:58 PM

Cheers mate.

tried it from the application object and it did the trick


I had been trying with application.GetWindow("Open") but weirdly, was returning null

Jun 7, 2011 at 7:50 AM



I have written a small function to manage finding Windows .


/// <summary>
/// Gets the window.
/// </summary>
/// <param name="titleOrID">The title or ID.</param>
/// <param name="type">The type.</param>
/// <param name="tryCounter">The try counter.</param>
/// <returns></returns>
public static Window GetWindow( string titleOrID, CheckType type, int tryCounter )
  Window window = null;
  Predicate<Window> predicate;
  if(type == CheckType.ByName)
	predicate = ( p => p.Name == titleOrID );
	predicate = ( p => p.AutomationElement.Current.AutomationId == titleOrID );

  List<Window> windows = MyApplication.GetWindows();
  //get Window
  int counter = 0;
  while (!windows.Exists(predicate) && counter < tryCounter)
	windows = MyApplication.GetWindows();
  if (type == CheckType.ByName)
	if (windows.Exists(p => p.Name == titleOrID))
	  window = windows.Where( p => p.Name == titleOrID ).First();
	if (windows.Exists(p => p.AutomationElement.Current.AutomationId == titleOrID))
	  window =  windows.Where( p => p.AutomationElement.Current.AutomationId == titleOrID ).First();
  return window;


Jun 9, 2011 at 2:01 PM
Edited Jun 9, 2011 at 2:02 PM

great, thanks mate. I will work with that. Will make handling Windows a lot easier

Just our of curiosity though, you wouldnt know why the GetWindow("Open") doesnt find it and getWindows().Last() does??

Jun 9, 2011 at 3:09 PM

no sorry,

I think this operation is only for finding the main window, but I'm not sure.



Jun 16, 2011 at 10:08 PM

Hey Throndorin

still working on the notepad stuff.

i wrote some code to find the Open button on the modal and click on it

             var open = SearchCriteria.ByText("Open");

this returns a null pointer on the click. I tried the automationId for the Open button as well (id = 1)

Any ideas what it could be?

I can use the keyboard class and press Return through that to mimic the "enter" operation but would be great if i could get the open button bit sorted

Thanks mate

Jun 17, 2011 at 10:01 AM

OS ist Windows 7 right?

I have the same problem, and use the same workaround.


The Fileopen or close dialog is strange :)  There are some more hints on it.....



Jun 17, 2011 at 10:27 AM

yeah mate, the OS is windows 7

another thing i noticed was that if i have an existing file open and i try to save an existing file using Save As, if i change the file name to lets say demo.txt, it doesnt seem to recognize it and says do you want to override the existing file.