windows 在 VB.net 中获取 shell 命令的输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8809194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:06:08  来源:igfitidea点击:

Get the output of a shell Command in VB.net

vb.netwindowsshell

提问by David Brunelle

I have a VB.net program in which I call the Shell function. I would like to get the text output that is produced from this code in a file. However, this is not the return value of the executed code so I don't really know how to.

我有一个 VB.net 程序,我在其中调用 Shell 函数。我想在文件中获取从此代码生成的文本输出。但是,这不是执行代码的返回值,所以我真的不知道该怎么做。

This program is a service but has access to the disk no problem as I already log other information. The whole service has multiple threads so I must also make sure that when the file is written it's not already accessed.

该程序是一项服务,但可以访问磁盘没有问题,因为我已经记录了其他信息。整个服务有多个线程,所以我还必须确保在写入文件时它尚未被访问。

回答by competent_tech

You won't be able to capture the output from Shell.

您将无法从 Shell 捕获输出。

You will need to change this to a process and you will need to capture the the Standard Output(and possibly Error) streams from the process.

您需要将其更改为流程,并且您需要从流程中捕获标准输出(可能还有错误)流。

Here is an example:

下面是一个例子:

        Dim oProcess As New Process()
        Dim oStartInfo As New ProcessStartInfo("ApplicationName.exe", "arguments")
        oStartInfo.UseShellExecute = False
        oStartInfo.RedirectStandardOutput = True
        oProcess.StartInfo = oStartInfo
        oProcess.Start()

        Dim sOutput As String
        Using oStreamReader As System.IO.StreamReader = oProcess.StandardOutput
            sOutput = oStreamReader.ReadToEnd()
        End Using
        Console.WriteLine(sOutput)

To get the standard error:

要获得标准错误:

'Add this next to standard output redirect
 oStartInfo.RedirectStandardError = True

'Add this below
Using oStreamReader As System.IO.StreamReader = checkOut.StandardError
        sOutput = oStreamReader.ReadToEnd()
End Using

回答by MarkJ

Just pipe the output to a text file?

只是将输出通过管道传输到文本文件?

MyCommand > "c:\file.txt"

Then read the file.

然后读取文件。

回答by Scotty Stultz

    Dim proc As New Process

    proc.StartInfo.FileName = "C:\ipconfig.bat"   
    proc.StartInfo.UseShellExecute = False
    proc.StartInfo.RedirectStandardOutput = True
    proc.Start()
    proc.WaitForExit()

    Dim output() As String = proc.StandardOutput.ReadToEnd.Split(CChar(vbLf))
    For Each ln As String In output
        RichTextBox1.AppendText(ln & vbNewLine)
        lstScan.Items.Add(ln & vbNewLine)
    Next

======================================================================= create a batch file in two lines as shown below:

================================================== ====================== 创建一个批处理文件,分两行,如下所示:

    echo off
    ipconfig

' make sure you save this batch file as ipconfig.bat or whatever name u decide to pick but make sure u put dot bat at the end of it.

' 确保将此批处理文件保存为 ipconfig.bat 或您决定选择的任何名称,但请确保将 dot bat 放在它的末尾。