Question

I try this tutorial

http://elegantcode.com/2010/02/20/silverlight-4-com-interop-and-the-cool-stuff-you-can-do-with-it/

I need open Notepad and send text into. I use this code:

using (dynamic shell = AutomationFactory.CreateObject("WScript.Shell"))
    {
        shell.Run(@"C:\windows\notepad.exe"); //you can open anything

        shell.SendKeys(txtTextToSend.Text);
    }

When I start aplication the notepad has appeared but there is no text in it.

What do I wrong. My system is W7 64bit. Thanks

Was it helpful?

Solution

The problem is that Run is a non-blocking operation that will return as soon as it has spun up the process. The UI for notepad probably hasn't appeared and put the focus into its input area by the time your code is calling SendKeys.

Try introducing a Thread.Sleep for a couple of seconds to prove whether this is true.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top