Vb.NET Start Process and return output

时间:2019-03-17 22:59:26

标签: vb.net process

I'm trying to execute some command line commands (putty). Now it may be that the command line waits for an input (simplest example: password maybe wrong). All the output should be written into Console.

My code displays a non-ending command line that does not write anything to the console.

Imports System.Text

Public Class Form1
    Private Shared processOutput As StringBuilder = Nothing
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim line As String = "/c plink -ssh chef@192.168.178.35 -pw 12345678 -m C:\putty\pw\putty.txt"
        processOutput = New StringBuilder()
        Dim objP As New System.Diagnostics.Process()
        Dim objPi As ProcessStartInfo = New ProcessStartInfo()
        With objPi
            .FileName = "cmd.exe"
            .Arguments = line
            .RedirectStandardOutput = True
            .RedirectStandardError = True
            .RedirectStandardInput = True
            .UseShellExecute = False
            .WindowStyle = ProcessWindowStyle.Hidden
            .CreateNoWindow = False
        End With
        objP.StartInfo = objPi

        AddHandler objP.OutputDataReceived, AddressOf OutputHandler
        objP.Start()
        objP.BeginOutputReadLine()
        objP.WaitForExit(1000)
        Debug.WriteLine(processOutput.ToString())
    End Sub

    Private Shared Sub OutputHandler(sendingProcess As Object, outLine As DataReceivedEventArgs)
        If Not String.IsNullOrEmpty(outLine.Data) Then
            processOutput.AppendLine(outLine.Data)
        End If
    End Sub


End Class

2 个答案:

答案 0 :(得分:0)

这是因为您没有为控制台或UI编写任何内容。 Debug.write与console.write不同

此外,您的委托函数可能会在1秒后返回。更好的范例是使用await。

Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
    ' Call the method that runs asynchronously.  
    Dim result As String = Await WaitAsynchronouslyAsync()  

    ' Call the method that runs synchronously.  
    'Dim result As String = Await WaitSynchronously()  

    ' Display the result.  
    TextBox1.Text &= result  
End Sub  

' The following method runs asynchronously. The UI thread is not  
' blocked during the delay. You can move or resize the Form1 window   
' while Task.Delay is running.  
Public Async Function WaitAsynchronouslyAsync() As Task(Of String)  
    Await Task.Delay(10000)  
    Return "Finished"  
End Function  

' The following method runs synchronously, despite the use of Async.  
' You cannot move or resize the Form1 window while Thread.Sleep  
' is running because the UI thread is blocked.  
Public Async Function WaitSynchronously() As Task(Of String)  
    ' Import System.Threading for the Sleep method.  
    Thread.Sleep(10000)  
    Return "Finished"  
End Function

https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/await-operator

答案 1 :(得分:0)

正如Stephen所提到的,您不会在控制台上打印任何内容,而只是在写入StringBuilder对象。因此,您必须将StringBuilder对象的内容打印到控制台上才能实际看到结果:

this

MSDN示例,请参见: https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.process.outputdatareceived?view=netframework-4.7.2