Silverlight 4 - 将文本发送到记事本
我尝试这个教程
http://elegantcode.com/2010/02/20/silverlight-4-com-interop-and-the-cool-stuff-you-can-do-with-it/
我需要打开记事本并将文本发送到。我使用此代码:
using (dynamic shell = AutomationFactory.CreateObject("WScript.Shell"))
{
shell.Run(@"C:\windows\notepad.exe"); //you can open anything
shell.SendKeys(txtTextToSend.Text);
}
当我启动应用程序时,记事本已出现,但其中没有文本。
我有什么错。我的系统是W7 64位。 谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是 Run 是一个非阻塞操作,一旦启动进程就会返回。当您的代码调用
SendKeys
时,记事本的 UI 可能尚未出现并将焦点放入其输入区域。尝试引入 Thread.Sleep 几秒钟来证明这是否正确。
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.