2012-03-01 45 views
0

我試圖在VB.NET中重定向命令行應用程序的輸出,並且出於某種原因它無法重定向輸出。這裏是我的代碼:VB.NET進程重定向輸出不工作

Dim myProcess As Process = New Process 
    myProcess.StartInfo.FileName = "g++" 
    myProcess.StartInfo.Arguments = CMDLineCommand 
    myProcess.StartInfo.UseShellExecute = False 
    myProcess.StartInfo.RedirectStandardOutput = True 
    myProcess.StartInfo.RedirectStandardError = True 
    myProcess.StartInfo.CreateNoWindow = True 
    myProcess.Start() 
    Dim output As String = myProcess.StandardOutput.ReadToEnd 
    myProcess.WaitForExit() 
    CMDLineOutputTextBox.Text = output 

有沒有人知道爲什麼它沒有被重定向?提前致謝!

-Neil

編輯:這裏是我完整的代碼,萬一有什麼奇怪的與我的語法:

 Dim myProcess As Process = New Process 
     myProcess.StartInfo.FileName = "g++" 
     myProcess.StartInfo.Arguments = CMDLineCommand 
     myProcess.StartInfo.UseShellExecute = False 
     myProcess.StartInfo.RedirectStandardOutput = True 
     myProcess.StartInfo.RedirectStandardError = True 
     myProcess.StartInfo.CreateNoWindow = True 
     myProcess.EnableRaisingEvents = True 
     AddHandler myProcess.OutputDataReceived, AddressOf GotData 
     myProcess.Start() 
     CMDLineOutputTextBox.Text = "" 
     myProcess.BeginOutputReadLine() 

後來......

Private Sub GotData(sendingProcess As Object, outLine As DataReceivedEventArgs) 
     If Not String.IsNullOrEmpty(outLine.Data) Then 
      SetText(outLine.Data) 
     End If 
End Sub 

Delegate Sub SetTextCallback(value As String) 
Private Sub SetText(ByVal value As String) 
    If Me.CMDLineOutputTextBox.InvokeRequired Then 
     Dim d As New SetTextCallback(AddressOf SetText) 
     Me.Invoke(d, New Object() {value}) 
    Else 
     Me.CMDLineOutputTextBox.Text += value + Environment.NewLine 
    End If 
End Sub 

什麼奇怪的?

回答

0

你的方法可以工作,當你在你讀到輸出的那一行結束時,所有的輸出都在那裏。既然你使用的是g ++,我認爲並不總是如此。使用OutputDataReceived事件並從中捕獲數據可能會更好。

Dim myProcess As Process = New Process 
myProcess.StartInfo.FileName = "ping" 
myProcess.StartInfo.Arguments = "www.google.com" 
myProcess.StartInfo.UseShellExecute = False 
myProcess.StartInfo.RedirectStandardOutput = True 
myProcess.StartInfo.RedirectStandardError = True 
myProcess.StartInfo.CreateNoWindow = True 
myProcess.EnableRaisingEvents = True 
AddHandler myProcess.OutputDataReceived, AddressOf GotData 
myProcess.Start() 
myProcess.BeginOutputReadLine() 

然後你處理該事件是這樣的:

Private Sub GotData(sendingProcess As Object, outLine As DataReceivedEventArgs) 
    If Not String.IsNullOrEmpty(outLine.Data) Then 
     SetText(outLine.Data) 
    End If 
End Sub 

Delegate Sub SetTextCallback(value As String) 
Private Sub SetText(ByVal value As String) 
    If Me.TextBox3.InvokeRequired Then 
     Dim d As New SetTextCallback(AddressOf SetText) 
     Me.Invoke(d, New Object() {value}) 
    Else 
     Me.TextBox3.Text += value + Environment.NewLine 
    End If 
End Sub 
+0

我在上面添加了我的完整代碼...出於某種原因,它仍然不在CMDLineOutputTextBox文本框中顯示命令行輸出:P – neilf 2012-03-05 05:17:51

+0

在SetText方法上設置斷點並確保它到達那裏。你的文本框設置爲多行嗎? – 2012-03-06 15:20:02

+0

是的,文本框是多行的。 – neilf 2012-03-08 03:23:53

0

我已經注意到了waitforExit似乎invoke.required檢查期間,使密碼鎖。當我拿出waitforexit時,它就可以工作。

+1

你的意思是waitForExit實際上等待? :) – eckes 2014-11-18 16:56:16