【问题标题】:Run CMD in VB.Net using files as StandardInput and StandardOutput使用文件作为 StandardInput 和 StandardOutput 在 VB.Net 中运行 CMD
【发布时间】:2020-06-02 15:41:47
【问题描述】:

我正在尝试在 VB.Net 中运行一个命令,例如:

my_program.exe < input_commands.txt > console_outputs.txt

我尝试使用 RedirectStandardInput :

myProcessStartInfo.FileName = programPath
myProcessStartInfo.RedirectStandardInput = True
myprocess.StartInfo = myProcessStartInfo
myprocess.Start()

还有一个 StreamWriter 来输入我的文本文件:

Dim myStreamWriter As StreamWriter = myprocess.StandardInput
For Each Line As String In System.IO.File.ReadLines(processInputFile)
    myStreamWriter.WriteLine(Line)
Next
myStreamWriter.Close()

还有一种类似的方法来获取文件文本的输出:

startInfo.RedirectStandardOutput = True
Dim output As String = Process.StandardOutput.ReadToEnd()

但结果非常有限......

您能指导我找到正确的解决方案吗?

【问题讨论】:

    标签: vb.net cmd process


    【解决方案1】:

    我要做的是,首先将input_commands.txt文件中的所有行读入mem:

    Private Sub ReadInputFile(programPath as String, processInputFile as String, processOuputFile as String)
    
            ' Open the file to read from.
            Dim readText() As String = File.ReadAllLines(processInputFile)
    
            For Each s As String In readText
    
                ' Run the command through the cmd process
                OutPutResults(programPath, s, processOuputFile)
    
            Next
    
    End Sub
    

    并将结果输出(附加)到console_output.txt

    Private Sub OutPutResults(programPath as String, strArgument as String, processOuputFile as String)
    
                Dim p As New Process()
    
                ' Set it to run hidden from user
                With p.StartInfo
                    .RedirectStandardOutput = True
                    .RedirectStandardError = True
                    .FileName = programPath
                    .Arguments = strArgument
                    .UseShellExecute = False
                    .CreateNoWindow = True
                End With
    
                p.Start()
    
                ' Save all output to the variable "strOutput"
                Dim strOutput As String = p.StandardOutput.ReadToEnd()
    
                ' Wait for programPath .exe to finish before we handle it's output:
                ' (Sync method, meaning the thread won't continue until this one is finished. Use .exited if wanting to do Async)
                ' Also note that you can add a timeout in milliseconds to this if wanted. i.e. .WaitForExit(1000)
                p.WaitForExit()
    
                ' Save the output to console_output.txt
                File.AppendAllText(processOuputFile, strOutput)
    
    End Sub
    

    注意上面的代码是完全未经测试的;我只是为了给你一个想法。

    【讨论】:

    • 谢谢。经过测试,执行停留在Dim strOutput As String = p.StandardOutput.ReadToEnd()
    【解决方案2】:

    我写了下面的函数来解决这个问题:

     Sub runProgrammCommandsInputFileResultsOutputFile(ByVal myProcessStartInfo As ProcessStartInfo, ByVal myProcess As Process, ByVal programPath As String, ByVal commandsInputFile As String, ByVal resultsOutputFile As String)
            myProcessStartInfo.FileName = programPath
            myProcessStartInfo.RedirectStandardInput = True
            myProcessStartInfo.RedirectStandardOutput = True
            myProcessStartInfo.RedirectStandardError = True
            myProcessStartInfo.UseShellExecute = False
            myProcessStartInfo.CreateNoWindow = True
            myProcess.StartInfo = myProcessStartInfo
    
            myProcess.Start()
    
            ' Read commands from text file
            Dim readCommandsInputFile() As String = System.IO.File.ReadAllLines(commandsInputFile)
    
            ' Open StreamWriter to StandardInput
            Dim myStreamWriter As StreamWriter = myProcess.StandardInput
    
            ' Write read commands to Streamwriter openned to StandardInput
            For Each s As String In readCommandsInputFile
                myStreamWriter.WriteLine(s)
            Next
    
            myStreamWriter.Close()
    
            'Read StandardOutput
            Dim resultsOutputStrings As String = myProcess.StandardOutput.ReadToEnd()
    
            ' Write StandardOutput read to text file
            System.IO.File.AppendAllText(resultsOutputFile, resultsOutputStrings)
        End Sub
    

    【讨论】:

      猜你喜欢
      • 2018-11-13
      • 1970-01-01
      • 2017-08-14
      • 2021-11-19
      • 1970-01-01
      • 2018-04-27
      • 2014-03-26
      • 2015-04-19
      • 2013-07-02
      相关资源
      最近更新 更多