How to read Console buffer in VBNET?

前端 未结 1 996
臣服心动
臣服心动 2021-01-29 12:37

I\'ve seen a C# example using ConsoleRead API function but when I\'ve tried to translate it to VBNET I get a lots of errors, also in other sites like pinvoke the unique example

相关标签:
1条回答
  • 2021-01-29 13:36

    Use

    StandardOutput.Read
    

    I think that the answer is the same as for your other question Run a commandline process and get the output while that process still running?

    Edit you are asking for an form application to read console output. So let's have a console application ():

    Module Module1
    
        Sub Main()
            While True
                ' data = Console.ReadKey.KeyChar
                Dim Generator As System.Random = New System.Random()
                Console.Write(Generator.Next(0, 100) & " ")
                Threading.Thread.Sleep(1000)
            End While
        End Sub
    
    End Module 
    

    It generates a space separated numbers one per second. Now the forms application. Let's have a form with a multiline TextBox named txtResult and a button cmdStart :

    Private Sub cmdStart_Click(sender As Object, e As EventArgs) Handles cmdStart.Click
            Dim ProcInfo As New ProcessStartInfo With
                {.FileName = "DataApp.exe", .RedirectStandardOutput = True, .UseShellExecute = False}
            Dim proc As Process = Process.Start(ProcInfo)
    
            While Not proc.HasExited
                Dim a As String = ChrW(proc.StandardOutput.Read)
                If a = " " Then
                    txtResult.Text &= vbCrLf
                Else
                    txtResult.Text &= a
                End If
                Threading.Thread.Sleep(100)
                Application.DoEvents()
            End While
    
        End Sub
    

    It writes the numbers to the TextBox one per a line. There is no API magic but it works.

    0 讨论(0)
提交回复
热议问题