vb.net讀取另一程序的標準輸出

 訪問一個進程的標準化輸出

 Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Dim p = New Process

        p.StartInfo.FileName = "ping"
        p.StartInfo.Arguments = "www.baidu.net"
        ' 必須禁用操作系統外殼程序  
        p.StartInfo.UseShellExecute = False
        p.StartInfo.CreateNoWindow = True
        p.StartInfo.RedirectStandardOutput = True

        p.Start()
        Dim output = p.StandardOutput.ReadToEnd()


        If (String.IsNullOrEmpty(output) = False) Then
            TextBox1.AppendText(output + vbCrLf)
        End If
        p.WaitForExit()
        p.Close()
    End Sub

在進程的waitForExit期間,主程序是被等待的。可以把進程放到後臺運行,新建一個BackgroundWorker部件,在按鈕按下時運行

 Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        BackgroundWorker1.RunWorkerAsync()
    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Dim p = New Process

        p.StartInfo.FileName = "ping"
        p.StartInfo.Arguments = "www.baidu.net"
        ' 必須禁用操作系統外殼程序  
        p.StartInfo.UseShellExecute = False
        p.StartInfo.CreateNoWindow = True
        p.StartInfo.RedirectStandardOutput = True

        p.Start()
        Dim output = p.StandardOutput.ReadToEnd()


        If (String.IsNullOrEmpty(output) = False) Then
            Me.Invoke(Sub() TextBox1.AppendText(output + vbCrLf))
        End If
        p.WaitForExit()
        p.Close()
    End Sub

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章