在 VB.net 中获取 shell 命令的输出 [英] Get the output of a shell Command in VB.net

查看:118
本文介绍了在 VB.net 中获取 shell 命令的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

推荐答案

您将无法从 Shell 捕获输出.

You won't be able to capture the output from 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.

这是一个例子:

        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)

获取标准错误:

'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

这篇关于在 VB.net 中获取 shell 命令的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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