2011-08-07 198 views
1

其實我想顯示命令提示符一樣,如果我這樣做顯示的信息:顯示消息

平google.com -t

下面的消息會顯示在命令提示符:

Reply from 74.125.235.17: bytes=32 time=133ms TTL=51 
Reply from 74.125.235.17: bytes=32 time=130ms TTL=51 
Reply from 74.125.235.17: bytes=32 time=130ms TTL=51 
Reply from 74.125.235.17: bytes=32 time=130ms TTL=51 

Ping statistics for 74.125.235.17: 
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), 
Approximate round trip times in milli-seconds: 
Minimum = 130ms, Maximum = 133ms, Average = 130ms 

我想顯示的確切信息到即時當它顯示在整個過程完成後不命令提示符我的程序的列表框中。我怎麼能這樣做?任何幫助?我正在使用C#/ vb.net。

ping google.com -t一樣,我想立即在列表框中顯示每條回覆消息。

回答

1

試試這個:

Private Results As String 

'The "Delegate" is used to correct the threading issue (Can't update control directly in VB.net 08/10), and invokes the needed text update. 
Private Delegate Sub delUpdate() 
Private Finished As New delUpdate(AddressOf UpdateText) 

Private Sub UpdateText() 
resultsTextBox.Text = Results 
End Sub 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 

Dim CMDThread As New Threading.Thread(AddressOf CMDAutomate) 
CMDThread.Start() 
End Sub 

Private Sub CMDAutomate() 

Dim myprocess As New Process 
Dim StartInfo As New System.Diagnostics.ProcessStartInfo 

'Starts the CMD Prompt 
StartInfo.FileName = "cmd.exe" 
StartInfo.RedirectStandardInput = True 
StartInfo.RedirectStandardOutput = True 

'Required to redirect 
StartInfo.UseShellExecute = False 

'Disables the creation of a CMD Prompt outside application. 
StartInfo.CreateNoWindow = True 


myprocess.StartInfo = StartInfo 
myprocess.Start() 
Dim SR As System.IO.StreamReader = myprocess.StandardOutput 
Dim SW As System.IO.StreamWriter = myprocess.StandardInput 

'Runs the command you entered... 
SW.WriteLine(TextBox1.Text) 

'Exits CMD Prompt 
SW.WriteLine("exit") 

'Displayes the results... 
Results = SR.ReadToEnd 
SW.Close() 
SR.Close() 

'Invokes Finished delegate, which updates textbox with the results text 
Invoke(Finished) 
End Sub 
+0

提的是,你需要一個名爲resultsTextBox一個TEX框和一個名爲Button1 – Maysam

+0

按鈕時,它並沒有真正幫助我。看到我的編輯問題。 – nightfire001

+0

這是輸出,還有一些額外的行,你可以刪除它們http://i.stack.imgur.com/RByCO.png – Maysam

2

爲此,您需要利用async read on standardoutput ...又見this ...

Here你可以找到所描述的問題完整的源代碼的解決方案..它甚至考慮到stderr ...

其他有趣的資源:

+2

這個鏈接http://www.codeproject.com/KB/threads/ReadProcessStdoutStderr.aspx做了訣竅。太棒了,享受賞金,乾杯! –

0

爲您快速解決方案。