2011-02-13 103 views
2

我試試這個教程的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); 
    } 

當我開始aplication記事本已經出現,但在它裏面沒有文字。

我錯了什麼。我的系統是W7 64bit。 謝謝

回答

2

問題是,運行是一個非阻塞操作,只要它啓動了進程就會返回。記事本的用戶界面可能沒有出現,並在您的代碼調用SendKeys時將焦點放到其輸入區域。

嘗試引入Thread.Sleep幾秒鐘以證明這是否屬實。

+0

偉大的工作,謝謝你,你救了我的一天。 – Marcus082 2011-02-14 17:54:38