Silverlight 4 - send text to Notepad

2019-05-26 23:04发布

问题:

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

回答1:

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.