如何在Visual Basic中逐行获取命令提示符窗口的输出? [英] How to get Output of a Command Prompt Window line by line in Visual Basic?

查看:229
本文介绍了如何在Visual Basic中逐行获取命令提示符窗口的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图得到一个命令行输出一行一行,直到输出结束,但我不能这样做。我在我的窗体中使用它,这个代码执行点击一个按钮。

你能告诉我我的代码有什么问题吗?

I am trying to get a command line output line by line till the end of the output but I am not able to do so. I am using it in my Form and this code executes on click of a button.
Can you tell me whats wrong with my code?

Dim proc As ProcessStartInfo = New ProcessStartInfo("cmd.exe")
    Dim pr As Process
    proc.CreateNoWindow = True
    proc.UseShellExecute = False
    proc.RedirectStandardInput = True
    proc.RedirectStandardOutput = True
    pr = Process.Start(proc)
    pr.StandardInput.WriteLine("cd C:\sdk\platform-tools\")
    pr.StandardInput.WriteLine("adb help")
    Dim helpArray(20) as String
    For i as Integer 1 To 7
    helpArray(i) = pr.StandardOutput.ReadLine()
    Next
    pr.StandardOutput.Close()

程序在执行此代码时停止响应。

The program stops responding when this code is executed.

推荐答案

我做了一些研究。 adb帮助将输出写入STDERR。所以你需要像:

I've done some research. adb help writes output into STDERR. So you need something like:

    Dim proc As ProcessStartInfo = New ProcessStartInfo("cmd.exe")
    Dim pr As Process
    proc.CreateNoWindow = True
    proc.UseShellExecute = False
    proc.RedirectStandardInput = True
    proc.RedirectStandardOutput = True
    pr = Process.Start(proc)
    pr.StandardInput.WriteLine("C:\sdk\platform-tools")
    pr.StandardInput.WriteLine("adb help 2>&1")
    pr.StandardInput.Close()
    Console.WriteLine(pr.StandardOutput.ReadToEnd())
    pr.StandardOutput.Close()

来捕获它。

例如,如果调用ipconfig,则不需要2>& 1。

to catch it.
You need no 2>&1 if you call ipconfig, for example.

这篇关于如何在Visual Basic中逐行获取命令提示符窗口的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆