【问题标题】:Get output of external console app to RichTextBox获取外部控制台应用程序的输出到 RichTextBox
【发布时间】:2014-12-25 17:34:04
【问题描述】:

我使用 VB.Net Shell() 命令启动了一个我尚未创建的控制台应用程序。我想在我的表单中从那个控制台获取到 RichTextBox 的行。这应该是可能的,因为我已经看到很多应用程序可以做到这一点。请提供一些可能对我有帮助的信息或任何程序。我试图查看外部应用程序是否创建日志文件,但它没有。

我是这样开始的。我应该添加什么以及在哪里返回输出?

Try
        writer.WriteLine(RichTextBox1.Text)
        writer.Close()
        Shell(CurDir() & "\yelloeye.exe .\new.demo")
Catch ex As Exception
        MsgBox(ex.Message)
End Try

【问题讨论】:

  • 请花点时间仔细查看How to Ask
  • 我猜你明白我的意思了,我已经编辑了问题标题,请回答一些有用的信息!
  • 一些代码如何显示what you have tried
  • 以这种态度,你几乎不可能得到任何帮助,放松一下并发布你遇到问题的实际代码。
  • @TheBlueDog 实际上,您可以使用 Process.StartInfo.RedirectStandardOutput = True 然后在事件中接收输出。因为您直接获得输出,所以迂回和丢失数据的机会更少。

标签: vb.net console


【解决方案1】:

这是一个使用@Plutonix 提到的RedirectStandardOutput 属性的示例,它将ping SO 并在RichTextBox 中显示结果。只需替换 .exe 名称和参数(如果有)以满足您的需要。

Private Sub ShellToRTB()
    Dim p As New Process()
    p.StartInfo.FileName = "ping.exe"
    p.StartInfo.Arguments = "www.stackoverflow.com"
    p.StartInfo.UseShellExecute = False
    p.StartInfo.RedirectStandardOutput = True
    p.Start()
    RichTextBox1.Text = p.StandardOutput.ReadToEnd()
    p.WaitForExit()
End Sub

结果:

【讨论】:

    猜你喜欢
    • 2020-04-24
    • 2014-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多