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

2023-12-30

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

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


您将无法捕获 Shell 的输出。

您需要将其更改为流程,并且需要捕获标准输出 http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx#Y1596(可能还有错误)来自进程的流。

这是一个例子:

        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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 VB.net 中获取 shell 命令的输出 的相关文章

随机推荐